View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.properties;
17  
18  import static com.google.common.base.Preconditions.checkArgument;
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  import java.nio.charset.Charset;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.kuali.common.util.Mode;
25  import org.kuali.common.util.property.PropertyFormat;
26  
27  public final class Location {
28  
29  	public static final Mode DEFAULT_MISSING_MODE = Mode.ERROR;
30  	public static final boolean DEFAULT_CACHEABLE = false;
31  	public static final PropertyFormat DEFAULT_PROPERTY_FORMAT = PropertyFormat.NORMAL;
32  	public static final String DEFAULT_ENCODING = Charset.defaultCharset().toString();
33  
34  	private final Mode missingMode;
35  	private final String encoding;
36  	private final PropertyFormat format;
37  	private final String value;
38  	private final boolean cacheable;
39  
40  	public Location(String value) {
41  		this(value, DEFAULT_ENCODING, DEFAULT_MISSING_MODE, DEFAULT_PROPERTY_FORMAT);
42  	}
43  
44  	public Location(String value, String encoding) {
45  		this(value, encoding, DEFAULT_MISSING_MODE, DEFAULT_PROPERTY_FORMAT);
46  	}
47  
48  	public Location(String value, String encoding, boolean cacheable) {
49  		this(value, encoding, DEFAULT_MISSING_MODE, DEFAULT_PROPERTY_FORMAT, cacheable);
50  	}
51  
52  	public Location(String value, String encoding, Mode missingMode, PropertyFormat format) {
53  		this(value, encoding, DEFAULT_MISSING_MODE, DEFAULT_PROPERTY_FORMAT, DEFAULT_CACHEABLE);
54  	}
55  
56  	public Location(String value, String encoding, Mode missingMode, PropertyFormat format, boolean cacheable) {
57  		this.value = value;
58  		this.encoding = encoding;
59  		this.missingMode = missingMode;
60  		this.format = format;
61  		this.cacheable = cacheable;
62  		Builder.validate(this);
63  	}
64  
65  	public Mode getMissingMode() {
66  		return missingMode;
67  	}
68  
69  	public String getEncoding() {
70  		return encoding;
71  	}
72  
73  	public PropertyFormat getFormat() {
74  		return format;
75  	}
76  
77  	public String getValue() {
78  		return value;
79  	}
80  
81  	public boolean isCacheable() {
82  		return cacheable;
83  	}
84  
85  	private Location(Builder builder) {
86  		this.missingMode = builder.missingMode;
87  		this.encoding = builder.encoding;
88  		this.format = builder.format;
89  		this.value = builder.value;
90  		this.cacheable = builder.cacheable;
91  	}
92  
93  	public static Builder builder(String value) {
94  		return new Builder(value);
95  	}
96  
97  	/**
98  	 * Create a new {@code Location} identical to an existing location but with {@code newValue} for its value
99  	 */
100 	public static Builder builder(Location existing, String newValue) {
101 		return new Builder(newValue).cacheable(existing.isCacheable()).encoding(existing.getEncoding()).format(existing.getFormat()).missingMode(existing.getMissingMode());
102 	}
103 
104 	public static class Builder {
105 
106 		private final String value;
107 		private Mode missingMode = DEFAULT_MISSING_MODE;
108 		private String encoding = DEFAULT_ENCODING;
109 		private PropertyFormat format = DEFAULT_PROPERTY_FORMAT;
110 		private boolean cacheable = DEFAULT_CACHEABLE;
111 
112 		public Builder(String value) {
113 			this.value = value;
114 		}
115 
116 		public Builder missingMode(Mode missingMode) {
117 			this.missingMode = missingMode;
118 			return this;
119 		}
120 
121 		public Builder encoding(String encoding) {
122 			this.encoding = encoding;
123 			return this;
124 		}
125 
126 		public Builder format(PropertyFormat format) {
127 			this.format = format;
128 			return this;
129 		}
130 
131 		public Builder cacheable(boolean cacheable) {
132 			this.cacheable = cacheable;
133 			return this;
134 		}
135 
136 		public Location build() {
137 			Location instance = new Location(this);
138 			validate(instance);
139 			return instance;
140 		}
141 
142 		private static void validate(Location instance) {
143 			checkArgument(!StringUtils.isBlank(instance.value), "value cannot be blank");
144 			checkArgument(!StringUtils.isBlank(instance.encoding), "encoding cannot be blank");
145 			checkNotNull(instance.format, "format cannot be null");
146 			checkNotNull(instance.missingMode, "missingMode cannot be null");
147 		}
148 
149 		public Mode getMissingMode() {
150 			return missingMode;
151 		}
152 
153 		public void setMissingMode(Mode missingMode) {
154 			this.missingMode = missingMode;
155 		}
156 
157 		public String getEncoding() {
158 			return encoding;
159 		}
160 
161 		public void setEncoding(String encoding) {
162 			this.encoding = encoding;
163 		}
164 
165 		public PropertyFormat getFormat() {
166 			return format;
167 		}
168 
169 		public void setFormat(PropertyFormat format) {
170 			this.format = format;
171 		}
172 
173 		public boolean isCacheable() {
174 			return cacheable;
175 		}
176 
177 		public void setCacheable(boolean cacheable) {
178 			this.cacheable = cacheable;
179 		}
180 
181 		public String getValue() {
182 			return value;
183 		}
184 	}
185 
186 }