@@ -74,68 +74,47 @@ public static DirtyState of(Formatter formatter, File file) throws IOException {
7474 }
7575
7676 public static DirtyState of (Formatter formatter , File file , byte [] rawBytes ) {
77- return new Calculation (formatter , file , rawBytes ).calculateDirtyState ();
78- }
77+ String raw = new String (rawBytes , formatter .getEncoding ());
78+ // check that all characters were encodable
79+ String encodingError = EncodingErrorMsg .msg (raw , rawBytes , formatter .getEncoding ());
80+ if (encodingError != null ) {
81+ throw new IllegalArgumentException (encodingError );
82+ }
83+
84+ String rawUnix = LineEnding .toUnix (raw );
85+
86+ // enforce the format
87+ String formattedUnix = formatter .compute (rawUnix , file );
88+ // convert the line endings if necessary
89+ String formatted = formatter .computeLineEndings (formattedUnix , file );
90+
91+ // if F(input) == input, then the formatter is well-behaving and the input is clean
92+ byte [] formattedBytes = formatted .getBytes (formatter .getEncoding ());
93+ if (Arrays .equals (rawBytes , formattedBytes )) {
94+ return isClean ;
95+ }
96+
97+ // F(input) != input, so we'll do a padded check
98+ String doubleFormattedUnix = formatter .compute (formattedUnix , file );
99+ if (doubleFormattedUnix .equals (formattedUnix )) {
100+ // most dirty files are idempotent-dirty, so this is a quick-short circuit for that common case
101+ return new DirtyState (formattedBytes );
102+ }
79103
80- private static class Calculation {
81- private final Formatter formatter ;
82- private final File file ;
83- private final byte [] rawBytes ;
84- private final String raw ;
85-
86- private Calculation (Formatter formatter , File file , byte [] rawBytes ) {
87- this .formatter = formatter ;
88- this .file = file ;
89- this .rawBytes = rawBytes ;
90- this .raw = new String (rawBytes , formatter .getEncoding ());
91- // check that all characters were encodable
92- String encodingError = EncodingErrorMsg .msg (raw , rawBytes , formatter .getEncoding ());
93- if (encodingError != null ) {
94- throw new IllegalArgumentException (encodingError );
95- }
104+ PaddedCell cell = PaddedCell .check (formatter , file , rawUnix );
105+ if (!cell .isResolvable ()) {
106+ return didNotConverge ;
96107 }
97108
98- /**
99- * Calculates whether the given file is dirty according to a PaddedCell invocation of the given formatter.
100- * DirtyState includes the clean state of the file, as well as a warning if we were not able to apply the formatter
101- * due to diverging idempotence.
102- */
103- public DirtyState calculateDirtyState () {
104- String rawUnix = LineEnding .toUnix (raw );
105-
106- // enforce the format
107- String formattedUnix = formatter .compute (rawUnix , file );
108- // convert the line endings if necessary
109- String formatted = formatter .computeLineEndings (formattedUnix , file );
110-
111- // if F(input) == input, then the formatter is well-behaving and the input is clean
112- byte [] formattedBytes = formatted .getBytes (formatter .getEncoding ());
113- if (Arrays .equals (rawBytes , formattedBytes )) {
114- return isClean ;
115- }
116-
117- // F(input) != input, so we'll do a padded check
118- String doubleFormattedUnix = formatter .compute (formattedUnix , file );
119- if (doubleFormattedUnix .equals (formattedUnix )) {
120- // most dirty files are idempotent-dirty, so this is a quick-short circuit for that common case
121- return new DirtyState (formattedBytes );
122- }
123-
124- PaddedCell cell = PaddedCell .check (formatter , file , rawUnix );
125- if (!cell .isResolvable ()) {
126- return didNotConverge ;
127- }
128-
129- // get the canonical bytes
130- String canonicalUnix = cell .canonical ();
131- String canonical = formatter .computeLineEndings (canonicalUnix , file );
132- byte [] canonicalBytes = canonical .getBytes (formatter .getEncoding ());
133- if (!Arrays .equals (rawBytes , canonicalBytes )) {
134- // and write them to disk if needed
135- return new DirtyState (canonicalBytes );
136- } else {
137- return isClean ;
138- }
109+ // get the canonical bytes
110+ String canonicalUnix = cell .canonical ();
111+ String canonical = formatter .computeLineEndings (canonicalUnix , file );
112+ byte [] canonicalBytes = canonical .getBytes (formatter .getEncoding ());
113+ if (!Arrays .equals (rawBytes , canonicalBytes )) {
114+ // and write them to disk if needed
115+ return new DirtyState (canonicalBytes );
116+ } else {
117+ return isClean ;
139118 }
140119 }
141120}
0 commit comments