Skip to content
This repository was archived by the owner on Aug 31, 2019. It is now read-only.

Commit ec03af1

Browse files
committed
Initial commit
0 parents  commit ec03af1

21 files changed

+769
-0
lines changed

.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Maven generated files
2+
target
3+
*.jar
4+
5+
# Mac OSX generated files
6+
.DS_Store
7+
8+
# Eclipse generated files
9+
.classpath
10+
.project
11+
.settings
12+
13+
# Vim generated files
14+
*~
15+
*.swp
16+
17+
# XCode 3 and 4 generated files
18+
*.mode1
19+
*.mode1v3
20+
*.mode2v3
21+
*.perspective
22+
*.perspectivev3
23+
*.pbxuser
24+
*.xcworkspace
25+
xcuserdata
26+
*class

LICENSE.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2012 Steve Anton
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7+
of the Software, and to permit persons to whom the Software is furnished to do
8+
so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Settings
2+
========
3+
4+
General purpose library for providing base classes which encapsulate the idea of configurable settings.

pom.xml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>me.anxuiz</groupId>
4+
<artifactId>settings</artifactId>
5+
<version>0.1.0-SNAPSHOT</version>
6+
<name>Settings</name>
7+
<description>Library providing general-purpose classes for defining configurable settings.</description>
8+
<licenses>
9+
<license>
10+
<name>MIT License</name>
11+
<url>LICENSE.txt</url>
12+
<distribution>repo</distribution>
13+
</license>
14+
</licenses>
15+
<scm>
16+
<connection>scm:git:git@github.com:Anxuiz/Settings.git</connection>
17+
<developerConnection>scm:git:git@github.com:Anxuiz/Settings.git</developerConnection>
18+
<url>https://github.com/Anxuiz/Settings</url>
19+
</scm>
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.google.guava</groupId>
23+
<artifactId>guava</artifactId>
24+
<version>13.0</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>net.sourceforge.findbugs</groupId>
28+
<artifactId>jsr305</artifactId>
29+
<version>1.3.7</version>
30+
<scope>provided</scope>
31+
</dependency>
32+
</dependencies>
33+
<build>
34+
<sourceDirectory>${basedir}/src/main/java/</sourceDirectory>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-compiler-plugin</artifactId>
39+
<version>2.5.1</version>
40+
<configuration>
41+
<source>1.5</source>
42+
<target>1.5</target>
43+
</configuration>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
<properties>
48+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
49+
</properties>
50+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package me.anxuiz.settings;
2+
3+
import java.util.Collection;
4+
5+
public interface Setting {
6+
String getName();
7+
8+
Collection<String> getAliases();
9+
10+
Class<?> getScope();
11+
12+
String getSummary();
13+
14+
String getDescription();
15+
16+
Type getType();
17+
18+
Object getDefaultValue();
19+
20+
void setDefaultValue(Object newDefault) throws IllegalArgumentException;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package me.anxuiz.settings;
2+
3+
public interface SettingCallback {
4+
void notifyChange(Setting setting, Object oldValue, Object newValue);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package me.anxuiz.settings;
2+
3+
import java.util.List;
4+
5+
public interface SettingCallbackManager {
6+
List<SettingCallback> getCallbacks(Setting setting);
7+
8+
int getNumCallbacks(Setting setting);
9+
10+
boolean hasCallbacks(Setting setting);
11+
12+
boolean addCallback(Setting setting, SettingCallback callback);
13+
14+
int clearCallbacks(Setting setting);
15+
16+
boolean removeCallback(Setting setting, SettingCallback callback);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package me.anxuiz.settings;
2+
3+
import javax.annotation.Nullable;
4+
5+
6+
7+
public interface SettingManager {
8+
boolean hasValue(Setting setting);
9+
10+
@Nullable Object getValue(Setting setting);
11+
12+
Object getValue(Setting setting, Object defaultValue) throws IllegalArgumentException;
13+
14+
<T> T getValue(Setting setting, Class<T> typeClass) throws IllegalArgumentException;
15+
16+
<T> T getValue(Setting setting, Class<T> typeClass, T defaultValue) throws IllegalArgumentException;
17+
18+
void setValue(Setting setting, Object value);
19+
20+
void deleteValue(Setting setting);
21+
22+
SettingCallbackManager getCallbackManager();
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package me.anxuiz.settings;
2+
3+
import java.util.Collection;
4+
5+
import javax.annotation.Nullable;
6+
7+
8+
public interface SettingRegistry {
9+
@Nullable Setting get(String search, boolean includeAliases);
10+
Setting find(String search, boolean includeAliases) throws IllegalArgumentException;
11+
12+
Collection<Setting> getSettings();
13+
14+
boolean isRegistered(Setting setting);
15+
void register(Setting setting) throws IllegalArgumentException;
16+
boolean unregister(Setting setting);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package me.anxuiz.settings;
2+
3+
public interface Toggleable {
4+
Object getNextState(Object previous) throws IllegalArgumentException;
5+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package me.anxuiz.settings;
2+
3+
public interface Type {
4+
String getName();
5+
6+
boolean isInstance(Object obj);
7+
8+
String print(Object obj) throws IllegalArgumentException;
9+
10+
String serialize(Object obj) throws IllegalArgumentException;
11+
12+
Object parse(String raw) throws TypeParseException;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package me.anxuiz.settings;
2+
3+
public class TypeParseException extends Exception {
4+
private static final long serialVersionUID = 1L;
5+
6+
public TypeParseException() {
7+
super();
8+
}
9+
10+
public TypeParseException(String message) {
11+
super(message);
12+
}
13+
14+
public TypeParseException(String message, Throwable cause) {
15+
super(message, cause);
16+
}
17+
18+
public TypeParseException(Throwable cause) {
19+
super(cause);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package me.anxuiz.settings.base;
2+
3+
import me.anxuiz.settings.Setting;
4+
import me.anxuiz.settings.SettingManager;
5+
import me.anxuiz.settings.util.TypeUtil;
6+
7+
import com.google.common.base.Preconditions;
8+
9+
public abstract class AbstractSettingManager implements SettingManager {
10+
public boolean hasValue(Setting setting) {
11+
Preconditions.checkNotNull(setting);
12+
13+
return this.getValue(setting) != null;
14+
}
15+
16+
public Object getValue(Setting setting, Object defaultValue) throws IllegalArgumentException {
17+
Preconditions.checkNotNull(setting);
18+
Preconditions.checkNotNull(defaultValue);
19+
Preconditions.checkArgument(setting.getType().isInstance(defaultValue));
20+
21+
Object value = this.getValue(setting);
22+
if(value != null) {
23+
return value;
24+
} else {
25+
return defaultValue;
26+
}
27+
}
28+
29+
public <T> T getValue(Setting setting, Class<T> typeClass) throws IllegalArgumentException {
30+
Preconditions.checkNotNull(setting);
31+
Preconditions.checkNotNull(typeClass);
32+
33+
Object rawValue = this.getValue(setting);
34+
if(rawValue != null) {
35+
return TypeUtil.getValue(rawValue, typeClass);
36+
} else {
37+
return null;
38+
}
39+
}
40+
41+
public <T> T getValue(Setting setting, Class<T> typeClass, T defaultValue) throws IllegalArgumentException {
42+
Preconditions.checkNotNull(setting);
43+
Preconditions.checkNotNull(typeClass);
44+
Preconditions.checkNotNull(defaultValue);
45+
46+
T value = this.getValue(setting, typeClass);
47+
if(value != null) {
48+
return value;
49+
} else {
50+
return defaultValue;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)