1
1
/*
2
- * Copyright 2016 DiffPlug
2
+ * Copyright 2016-2023 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
39
39
public final class Formatter implements Serializable , AutoCloseable {
40
40
private static final long serialVersionUID = 1L ;
41
41
42
+ // The name is used for logging purpose. It does not convey any applicative purpose
43
+ private String name ;
42
44
private LineEnding .Policy lineEndingsPolicy ;
43
45
private Charset encoding ;
44
46
private Path rootDir ;
45
47
private List <FormatterStep > steps ;
46
48
private FormatExceptionPolicy exceptionPolicy ;
47
49
48
- private Formatter (LineEnding .Policy lineEndingsPolicy , Charset encoding , Path rootDirectory , List <FormatterStep > steps , FormatExceptionPolicy exceptionPolicy ) {
50
+ private Formatter (String name , LineEnding .Policy lineEndingsPolicy , Charset encoding , Path rootDirectory , List <FormatterStep > steps , FormatExceptionPolicy exceptionPolicy ) {
51
+ this .name = name ;
49
52
this .lineEndingsPolicy = Objects .requireNonNull (lineEndingsPolicy , "lineEndingsPolicy" );
50
53
this .encoding = Objects .requireNonNull (encoding , "encoding" );
51
54
this .rootDir = Objects .requireNonNull (rootDirectory , "rootDir" );
@@ -55,6 +58,7 @@ private Formatter(LineEnding.Policy lineEndingsPolicy, Charset encoding, Path ro
55
58
56
59
// override serialize output
57
60
private void writeObject (ObjectOutputStream out ) throws IOException {
61
+ out .writeObject (name );
58
62
out .writeObject (lineEndingsPolicy );
59
63
out .writeObject (encoding .name ());
60
64
out .writeObject (rootDir .toString ());
@@ -65,6 +69,7 @@ private void writeObject(ObjectOutputStream out) throws IOException {
65
69
// override serialize input
66
70
@ SuppressWarnings ("unchecked" )
67
71
private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
72
+ name = (String ) in .readObject ();
68
73
lineEndingsPolicy = (LineEnding .Policy ) in .readObject ();
69
74
encoding = Charset .forName ((String ) in .readObject ());
70
75
rootDir = Paths .get ((String ) in .readObject ());
@@ -78,6 +83,10 @@ private void readObjectNoData() throws ObjectStreamException {
78
83
throw new UnsupportedOperationException ();
79
84
}
80
85
86
+ public String getName () {
87
+ return name ;
88
+ }
89
+
81
90
public LineEnding .Policy getLineEndingsPolicy () {
82
91
return lineEndingsPolicy ;
83
92
}
@@ -103,6 +112,8 @@ public static Formatter.Builder builder() {
103
112
}
104
113
105
114
public static class Builder {
115
+ // optional parameters
116
+ private String name = "misc" ;
106
117
// required parameters
107
118
private LineEnding .Policy lineEndingsPolicy ;
108
119
private Charset encoding ;
@@ -112,6 +123,11 @@ public static class Builder {
112
123
113
124
private Builder () {}
114
125
126
+ public Builder name (String name ) {
127
+ this .name = name ;
128
+ return this ;
129
+ }
130
+
115
131
public Builder lineEndingsPolicy (LineEnding .Policy lineEndingsPolicy ) {
116
132
this .lineEndingsPolicy = lineEndingsPolicy ;
117
133
return this ;
@@ -138,7 +154,7 @@ public Builder exceptionPolicy(FormatExceptionPolicy exceptionPolicy) {
138
154
}
139
155
140
156
public Formatter build () {
141
- return new Formatter (lineEndingsPolicy , encoding , rootDir , steps ,
157
+ return new Formatter (name , lineEndingsPolicy , encoding , rootDir , steps ,
142
158
exceptionPolicy == null ? FormatExceptionPolicy .failOnlyOnError () : exceptionPolicy );
143
159
}
144
160
}
@@ -248,6 +264,7 @@ public String compute(String unix, File file) {
248
264
public int hashCode () {
249
265
final int prime = 31 ;
250
266
int result = 1 ;
267
+ result = prime * result + name .hashCode ();
251
268
result = prime * result + encoding .hashCode ();
252
269
result = prime * result + lineEndingsPolicy .hashCode ();
253
270
result = prime * result + rootDir .hashCode ();
@@ -268,7 +285,8 @@ public boolean equals(Object obj) {
268
285
return false ;
269
286
}
270
287
Formatter other = (Formatter ) obj ;
271
- return encoding .equals (other .encoding ) &&
288
+ return name .equals (other .name ) &&
289
+ encoding .equals (other .encoding ) &&
272
290
lineEndingsPolicy .equals (other .lineEndingsPolicy ) &&
273
291
rootDir .equals (other .rootDir ) &&
274
292
steps .equals (other .steps ) &&
0 commit comments