|
| 1 | +package me.anxuiz.settings; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +import me.anxuiz.settings.base.SimpleSetting; |
| 7 | + |
| 8 | +import com.google.common.base.Preconditions; |
| 9 | +import com.google.common.collect.Sets; |
| 10 | + |
| 11 | +public class SettingBuilder { |
| 12 | + protected String name = null; |
| 13 | + protected final Set<String> aliases = Sets.newLinkedHashSet(); |
| 14 | + protected Class<?> scope = null; |
| 15 | + protected String summary = null; |
| 16 | + protected String description = null; |
| 17 | + protected Type type = null; |
| 18 | + protected Object defaultValue = null; |
| 19 | + |
| 20 | + public SettingBuilder() { |
| 21 | + } |
| 22 | + |
| 23 | + public SettingBuilder name(String name) { |
| 24 | + Preconditions.checkNotNull(name); |
| 25 | + this.name = name; |
| 26 | + return this; |
| 27 | + } |
| 28 | + |
| 29 | + public SettingBuilder alias(String alias) { |
| 30 | + Preconditions.checkNotNull(alias); |
| 31 | + this.aliases.add(alias); |
| 32 | + return this; |
| 33 | + } |
| 34 | + |
| 35 | + public SettingBuilder aliases(Collection<String> aliases) { |
| 36 | + Preconditions.checkNotNull(aliases); |
| 37 | + this.aliases.clear(); |
| 38 | + this.aliases.addAll(aliases); |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + public SettingBuilder scope(Class<?> scope) { |
| 43 | + Preconditions.checkNotNull(scope); |
| 44 | + this.scope = scope; |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + public SettingBuilder summary(String summary) { |
| 49 | + Preconditions.checkNotNull(summary); |
| 50 | + this.summary = summary; |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public SettingBuilder description(String description) { |
| 55 | + Preconditions.checkNotNull(description); |
| 56 | + this.description = description; |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + public SettingBuilder type(Type type) { |
| 61 | + Preconditions.checkNotNull(type); |
| 62 | + // clear the default value if it does not work with the new type |
| 63 | + if(this.defaultValue != null && !type.isInstance(this.defaultValue)) { |
| 64 | + this.defaultValue = null; |
| 65 | + } |
| 66 | + this.type = type; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + public SettingBuilder defaultValue(Object defaultValue) { |
| 71 | + Preconditions.checkNotNull(defaultValue); |
| 72 | + Preconditions.checkArgument(this.type != null && this.type.isInstance(defaultValue), "default value must be an instance of the type specified"); |
| 73 | + this.defaultValue = defaultValue; |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + public Setting get() throws IllegalStateException { |
| 78 | + Preconditions.checkState(this.name != null, "setting must have name"); |
| 79 | + Preconditions.checkState(this.scope != null, "setting must have scope"); |
| 80 | + Preconditions.checkState(this.summary != null, "setting must have summary"); |
| 81 | + Preconditions.checkState(this.type != null, "setting must have type"); |
| 82 | + Preconditions.checkState(this.defaultValue != null, "setting must have a default value"); |
| 83 | + |
| 84 | + return new SimpleSetting(this.name, this.aliases, this.scope, this.summary, this.description, this.type, this.defaultValue); |
| 85 | + } |
| 86 | +} |
0 commit comments