001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.properties; 017 018import static com.google.common.base.Preconditions.checkArgument; 019import static com.google.common.base.Preconditions.checkNotNull; 020 021import java.nio.charset.Charset; 022 023import org.apache.commons.lang3.StringUtils; 024import org.kuali.common.util.Mode; 025import org.kuali.common.util.property.PropertyFormat; 026 027public final class Location { 028 029 public static final Mode DEFAULT_MISSING_MODE = Mode.ERROR; 030 public static final boolean DEFAULT_CACHEABLE = false; 031 public static final PropertyFormat DEFAULT_PROPERTY_FORMAT = PropertyFormat.NORMAL; 032 public static final String DEFAULT_ENCODING = Charset.defaultCharset().toString(); 033 034 private final Mode missingMode; 035 private final String encoding; 036 private final PropertyFormat format; 037 private final String value; 038 private final boolean cacheable; 039 040 public Location(String value) { 041 this(value, DEFAULT_ENCODING, DEFAULT_MISSING_MODE, DEFAULT_PROPERTY_FORMAT); 042 } 043 044 public Location(String value, String encoding) { 045 this(value, encoding, DEFAULT_MISSING_MODE, DEFAULT_PROPERTY_FORMAT); 046 } 047 048 public Location(String value, String encoding, boolean cacheable) { 049 this(value, encoding, DEFAULT_MISSING_MODE, DEFAULT_PROPERTY_FORMAT, cacheable); 050 } 051 052 public Location(String value, String encoding, Mode missingMode, PropertyFormat format) { 053 this(value, encoding, DEFAULT_MISSING_MODE, DEFAULT_PROPERTY_FORMAT, DEFAULT_CACHEABLE); 054 } 055 056 public Location(String value, String encoding, Mode missingMode, PropertyFormat format, boolean cacheable) { 057 this.value = value; 058 this.encoding = encoding; 059 this.missingMode = missingMode; 060 this.format = format; 061 this.cacheable = cacheable; 062 Builder.validate(this); 063 } 064 065 public Mode getMissingMode() { 066 return missingMode; 067 } 068 069 public String getEncoding() { 070 return encoding; 071 } 072 073 public PropertyFormat getFormat() { 074 return format; 075 } 076 077 public String getValue() { 078 return value; 079 } 080 081 public boolean isCacheable() { 082 return cacheable; 083 } 084 085 private Location(Builder builder) { 086 this.missingMode = builder.missingMode; 087 this.encoding = builder.encoding; 088 this.format = builder.format; 089 this.value = builder.value; 090 this.cacheable = builder.cacheable; 091 } 092 093 public static Builder builder(String value) { 094 return new Builder(value); 095 } 096 097 /** 098 * Create a new {@code Location} identical to an existing location but with {@code newValue} for its value 099 */ 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}