22
33import javax .annotation .Nonnull ;
44import java .util .Objects ;
5+ import java .util .StringJoiner ;
56
67public final class DotenvConfigurer {
78
89 private static final class LazyHolder {
910 private static final DotenvConfigurer DEFAULT = new DotenvConfigurer (
10- /* strictMode = */ false ,
11- /* includeSystemVariables = */ true ,
12- /* allowOverrideSystemVariables = */ true ,
13- /* location = */ "." ,
14- /* filename = */ ".env"
11+ /* location = */ "." ,
12+ /* filename = */ ".env" ,
13+ /* strictMode = */ false ,
14+ /* includeSystemVariables = */ true ,
15+ /* replaceSystemVariables = */ true
1516 );
1617 }
1718
18- private final boolean strictMode ;
19- private final boolean includeSystemVariables ;
20- private final boolean allowOverrideSystemVariables ;
2119 private final String location ;
2220 private final String filename ;
21+ private final boolean strictMode ;
22+ private final boolean includeSystemVariables ;
23+ private final boolean replaceSystemVariables ;
2324
2425 private DotenvConfigurer (
26+ final String location ,
27+ final String filename ,
2528 final boolean strictMode ,
2629 final boolean includeSystemVariables ,
27- final boolean allowOverrideSystemVariables ,
28- final String location ,
29- final String filename
30+ final boolean replaceSystemVariables
3031 ) {
31- Objects .requireNonNull (location , "location must not be null" );
32- Objects .requireNonNull (filename , "filename must not be null" );
33-
32+ this .location = Objects .requireNonNull (location , "location must not be null" );
33+ this .filename = Objects .requireNonNull (filename , "filename must not be null" );
3434 this .strictMode = strictMode ;
3535 this .includeSystemVariables = includeSystemVariables ;
36- this .allowOverrideSystemVariables = allowOverrideSystemVariables ;
37- this .location = location ;
38- this .filename = filename ;
36+ this .replaceSystemVariables = replaceSystemVariables ;
3937 }
4038
4139 static DotenvConfigurer getInstance () {
@@ -45,70 +43,78 @@ static DotenvConfigurer getInstance() {
4543 @ Nonnull
4644 public Dotenv load () {
4745 try {
48- final var fileContent = DotenvFileLoader .readDotenvFileAsProperties (location , filename );
49- return new DotenvImpl (this , fileContent );
46+ final var properties = DotenvFileLoader .readDotenvFileAsProperties (location , filename );
47+ return new DotenvImpl (this , properties );
5048 } catch (final Exception e ) {
5149 throw new LoadingException (e );
5250 }
5351 }
5452
5553 @ Nonnull
56- public DotenvConfigurer strictMode ( final boolean strictMode ) {
54+ public DotenvConfigurer location ( @ Nonnull final String location ) {
5755 return new DotenvConfigurer (
58- strictMode ,
56+ location ,
57+ this .filename ,
58+ this .strictMode ,
5959 this .includeSystemVariables ,
60- this .allowOverrideSystemVariables ,
61- this .location ,
62- this .filename
60+ this .replaceSystemVariables
6361 );
6462 }
6563
6664 @ Nonnull
67- public DotenvConfigurer includeSystemVariables ( final boolean includeSystemVariables ) {
65+ public DotenvConfigurer filename ( @ Nonnull final String filename ) {
6866 return new DotenvConfigurer (
69- this .strictMode ,
70- includeSystemVariables ,
71- this .allowOverrideSystemVariables ,
7267 this .location ,
73- this .filename
68+ filename ,
69+ this .strictMode ,
70+ this .includeSystemVariables ,
71+ this .replaceSystemVariables
7472 );
7573 }
7674
77-
7875 @ Nonnull
79- public DotenvConfigurer allowOverrideSystemVariables (final boolean allowOverrideSystemVariables ) {
76+ public DotenvConfigurer strictMode (final boolean strictMode ) {
8077 return new DotenvConfigurer (
81- this .strictMode ,
82- this .includeSystemVariables ,
83- allowOverrideSystemVariables ,
8478 this .location ,
85- this .filename
79+ this .filename ,
80+ strictMode ,
81+ this .includeSystemVariables ,
82+ this .replaceSystemVariables
8683 );
8784 }
8885
89-
9086 @ Nonnull
91- public DotenvConfigurer location ( @ Nonnull final String location ) {
87+ public DotenvConfigurer includeSystemVariables ( final boolean includeSystemVariables ) {
9288 return new DotenvConfigurer (
89+ this .location ,
90+ this .filename ,
9391 this .strictMode ,
94- this .includeSystemVariables ,
95- this .allowOverrideSystemVariables ,
96- location ,
97- this .filename
92+ includeSystemVariables ,
93+ this .replaceSystemVariables
9894 );
9995 }
10096
10197 @ Nonnull
102- public DotenvConfigurer filename ( @ Nonnull final String filename ) {
98+ public DotenvConfigurer replaceSystemVariables ( final boolean replaceSystemVariables ) {
10399 return new DotenvConfigurer (
104- strictMode ,
105- includeSystemVariables ,
106- allowOverrideSystemVariables ,
107- location ,
108- filename
100+ this . location ,
101+ this . filename ,
102+ this . strictMode ,
103+ this . includeSystemVariables ,
104+ replaceSystemVariables
109105 );
110106 }
111107
108+ @ Nonnull
109+ public String getLocation () {
110+ return location ;
111+ }
112+
113+ @ Nonnull
114+ public String getFilename () {
115+ return filename ;
116+ }
117+
112118 public boolean isStrictMode () {
113119 return strictMode ;
114120 }
@@ -117,17 +123,46 @@ public boolean isIncludeSystemVariables() {
117123 return includeSystemVariables ;
118124 }
119125
120- public boolean isAllowOverrideSystemVariables () {
121- return allowOverrideSystemVariables ;
126+ public boolean isReplaceSystemVariables () {
127+ return replaceSystemVariables ;
122128 }
123129
124- @ Nonnull
125- public String getLocation () {
126- return location ;
130+ @ Override
131+ public boolean equals (final Object o ) {
132+ if (this == o ) {
133+ return true ;
134+ }
135+ if (o == null || getClass () != o .getClass ()) {
136+ return false ;
137+ }
138+
139+ DotenvConfigurer that = (DotenvConfigurer ) o ;
140+
141+ return strictMode == that .strictMode
142+ && includeSystemVariables == that .includeSystemVariables
143+ && replaceSystemVariables == that .replaceSystemVariables
144+ && location .equals (that .location )
145+ && filename .equals (that .filename );
127146 }
128147
129- @ Nonnull
130- public String getFilename () {
131- return filename ;
148+ @ Override
149+ public int hashCode () {
150+ int result = location .hashCode ();
151+ result = 31 * result + filename .hashCode ();
152+ result = 31 * result + (strictMode ? 1 : 0 );
153+ result = 31 * result + (includeSystemVariables ? 1 : 0 );
154+ result = 31 * result + (replaceSystemVariables ? 1 : 0 );
155+ return result ;
156+ }
157+
158+ @ Override
159+ public String toString () {
160+ return new StringJoiner (", " , DotenvConfigurer .class .getSimpleName () + "(" , ")" )
161+ .add ("location='" + location + "'" )
162+ .add ("filename='" + filename + "'" )
163+ .add ("strictMode=" + strictMode )
164+ .add ("includeSystemVariables=" + includeSystemVariables )
165+ .add ("replaceSystemVariables=" + replaceSystemVariables )
166+ .toString ();
132167 }
133168}
0 commit comments