pax_global_header00006660000000000000000000000064123645402660014522gustar00rootroot0000000000000052 comment=6f612baff7b76ecdd4583a70e01b4334fe07b2ef options-options-1.2/000077500000000000000000000000001236454026600145505ustar00rootroot00000000000000options-options-1.2/.gitignore000066400000000000000000000000101236454026600165270ustar00rootroot00000000000000target/ options-options-1.2/README.md000066400000000000000000000111041236454026600160240ustar00rootroot00000000000000options: a library for JVM property-driven configuration ======================================================== This library provides a simple mechanism for defining JVM property-based configuration for an application or library. Options are defined via a small DSL-like setup, supporting String, Integer, Boolean, and Enum-based configurations. Non-Boolean options support a set of supported values, and all options allow specifying a default value. In addition, options are created with an Enum-based category (provided by the user) and a documentation string, which allows grouping properties and printing out a full set of options as a valid, modifiable .properties file. Usage ----- A real-world usage example from the [JRuby project's Options class](https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/util/cli/Options.java). Example usage of the four types of options: ```java import com.headius.options.Option; import static com.headius.options.Option.*; // ... enum MyCategory { STANDARD, EXTENDED, SPECIAL, OTHER } enum AccountType { ADMIN, GUEST, NORMAL } // ... // Without defaults or options... Option databaseName = string("config.databaseName", MyCategory.STANDARD, "name of the database"); Option connCount = integer("config.connCount", MyCategory.EXTENDED, "connection count"); Option authenticate = bool("config.authenticate", MyCategory.SPECIAL, "do authentication"); Option acctType = enumeration("config.acctType", MyCategory.OTHER, AccountType.class, "account type"); // With options... Option username = string("config.username", MyCategory.STANDARD, new String[]{"root", "guest"}, "account name"); // With default... Option timeout = bool("config.timeout", MyCategory.EXTENDED, true, "timeout connections"); // With both... Option timeoutSecs = integer("config.timeoutSecs", MyCategory.EXTENDED, new Integer[]{15, 30, 60}, 30, "timeout in seconds"); // Load options; value is retrieved once and cached. String dbname = databaseName.load(); AccountType accountType = acctType.load(); // Or reloading the property can be forced... System.setProperty("config.databaseName", "customers"); dbname = databaseName.reload(); // options can be queried to see if they were provided if (databaseName.isSpecified()) { // logic for setting database name } // If your system has a standard property prefix, it can be specified in the // option. The prefix will be omitted from formatted output. Option firstName = string("config", "first.name", MyCategory.STANDARD, "..."); // formatted output of all options System.out.println( Option.formatOptions( databaseName, connCount, authenticate, acctType, username, timeout, timeoutSecs, firstName)); // short output of current settings System.out.println( Option.formatValues( databaseName, connCount, authenticate, acctType, username, timeout, timeoutSecs, firstName)); ``` Formatted output of options is an editable properties file. Note the "firstName" option does not include the "config." prefix. ``` ################################################################################ # STANDARD ################################################################################ # name of the database #config.databaseName= # account name # Options: [root, guest]. #config.username= # ... #first.name= ################################################################################ # EXTENDED ################################################################################ # connection count #config.connCount= # timeout connections # Options: [true, false], Default: true. #config.timeout=true # timeout in seconds # Options: [15, 30, 60], Default: 30. #config.timeoutSecs=30 ################################################################################ # SPECIAL ################################################################################ # do authentication # Options: [true, false]. #config.authenticate= ################################################################################ # OTHER ################################################################################ # account type # Options: [ADMIN, GUEST, NORMAL]. #config.acctType= ``` You can also format the values of all options, loaded on the current JVM. ``` STANDARD config.databaseName=customers config.username= first.name= EXTENDED config.connCount= config.timeout=true config.timeoutSecs=30 SPECIAL config.authenticate= OTHER config.acctType= ```options-options-1.2/pom.xml000066400000000000000000000027251236454026600160730ustar00rootroot00000000000000 4.0.0 com.headius options 1.2 jar options https://github.com/headius/options UTF-8 org.sonatype.oss oss-parent 7 The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt repo scm:git:https://github.com/headius/options.git scm:git:git@github.com:headius/options.git https://github.com/headius/options headius Charles Nutter headius@headius.com junit junit 4.11 test options-options-1.2/src/000077500000000000000000000000001236454026600153375ustar00rootroot00000000000000options-options-1.2/src/main/000077500000000000000000000000001236454026600162635ustar00rootroot00000000000000options-options-1.2/src/main/java/000077500000000000000000000000001236454026600172045ustar00rootroot00000000000000options-options-1.2/src/main/java/com/000077500000000000000000000000001236454026600177625ustar00rootroot00000000000000options-options-1.2/src/main/java/com/headius/000077500000000000000000000000001236454026600214045ustar00rootroot00000000000000options-options-1.2/src/main/java/com/headius/options/000077500000000000000000000000001236454026600230775ustar00rootroot00000000000000options-options-1.2/src/main/java/com/headius/options/BooleanOption.java000066400000000000000000000024221236454026600265120ustar00rootroot00000000000000/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.headius.options; /** * A Boolean-based Option. */ public class BooleanOption extends Option { public BooleanOption(String prefix, String name, Enum category, Boolean defval, String description) { super(prefix, name, Boolean.class, category, new Boolean[] {true, false}, defval, description); } public BooleanOption(String longName, Enum category, Boolean defval, String description) { super(longName, Boolean.class, category, new Boolean[] {true, false}, defval, description); } protected Boolean reloadValue() { String value = super.loadProperty(); if (value == null) { return defval; } return Boolean.valueOf(value); } } options-options-1.2/src/main/java/com/headius/options/EnumerationOption.java000066400000000000000000000025161236454026600274250ustar00rootroot00000000000000/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.headius.options; /** * An Enum-based Option. */ public class EnumerationOption> extends Option { public EnumerationOption(String prefix, String name, Enum category, Class enumType, T defval, String description) { super(prefix, name, enumType, category, (T[])enumType.getEnumConstants(), defval, description); } public EnumerationOption(String longName, Enum category, Class enumType, T defval, String description) { super(longName, enumType, category, (T[])enumType.getEnumConstants(), defval, description); } protected T reloadValue() { String value = super.loadProperty(); if (value == null) { return defval; } return Enum.valueOf((Class)type, value); } } options-options-1.2/src/main/java/com/headius/options/IntegerOption.java000066400000000000000000000032201236454026600265250ustar00rootroot00000000000000/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.headius.options; /** * An Integer-based Option. */ public class IntegerOption extends Option { public IntegerOption(String prefix, String name, Enum category, Integer[] options, Integer defval, String description) { super(prefix, name, Integer.class, category, options, defval, description); } public IntegerOption(String longName, Enum category, Integer[] options, Integer defval, String description) { super(longName, Integer.class, category, options, defval, description); } public IntegerOption(String prefix, String name, Enum category, Integer defval, String description) { super(prefix, name, Integer.class, category, null, defval, description); } public IntegerOption(String longName, Enum category, Integer defval, String description) { super(longName, Integer.class, category, null, defval, description); } public Integer reloadValue() { String value = super.loadProperty(); if (value == null) { return defval; } return Integer.parseInt(value); } } options-options-1.2/src/main/java/com/headius/options/Option.java000066400000000000000000000434401236454026600252170ustar00rootroot00000000000000/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.headius.options; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * Represents a single option, with a category, name, value type, * options, default value, and description. * * This type should be subclassed for specific types of values. * * @see StringOption * @see IntegerOption * @see BooleanOption * @see EnuerationOption * @see Option#string * @see Option#integer * @see Option#bool * @see Option#enumeration * * @param the type of value associated with the option */ public abstract class Option { /** * Create a new option with the given values. * * @param an enumeration type * @param prefix the prefix used for loading this option from properties * @param shortName the rest of the property name * @param type the value type of the option * @param category the category to which this option belongs * @param options a list of supported for the option, or null if the set is * not applicable * @param defval the default value for the option * @param description a description for the option */ public Option(String prefix, String shortName, Class type, Enum category, T[] options, T defval, String description) { this.category = category; this.prefix = prefix; this.shortName = shortName; this.longName = prefix + "." + shortName; this.displayName = shortName; this.type = type; this.options = options; this.defval = defval; this.description = description; this.specified = false; } /** * Create a new option with the given values. * * @param an enumeration type * @param longName the property name * @param type the value type of the option * @param category the category to which this option belongs * @param options a list of supported for the option, or null if the set is * not applicable * @param defval the default value for the option * @param description a description for the option */ public Option(String longName, Class type, Enum category, T[] options, T defval, String description) { this.category = category; this.prefix = null; this.shortName = null; this.longName = longName; this.displayName = longName; this.type = type; this.options = options; this.defval = defval; this.description = description; this.specified = false; } /** * Create a new String option with the given configuration. */ public static Option string(String prefix, String name, Enum category, String description) { return new StringOption(prefix, name, category, null, null, description); } /** * Create a new String option with the given configuration. */ public static Option string(String longName, Enum category, String description) { return new StringOption(longName, category, null, null, description); } /** * Create a new String option with the given configuration. */ public static Option string(String prefix, String name, Enum category, String defval, String description) { return new StringOption(prefix, name, category, null, defval, description); } /** * Create a new String option with the given configuration. */ public static Option string(String longName, Enum category, String defval, String description) { return new StringOption(longName, category, null, defval, description); } /** * Create a new String option with the given configuration. */ public static Option string(String prefix, String name, Enum category, String[] options, String description) { return new StringOption(prefix, name, category, options, null, description); } /** * Create a new String option with the given configuration. */ public static Option string(String longName, Enum category, String[] options, String description) { return new StringOption(longName, category, options, null, description); } /** * Create a new String option with the given configuration. */ public static Option string(String prefix, String name, Enum category, String[] options, String defval, String description) { return new StringOption(prefix, name, category, options, defval, description); } /** * Create a new String option with the given configuration. */ public static Option string(String longName, Enum category, String[] options, String defval, String description) { return new StringOption(longName, category, options, defval, description); } /** * Create a new Boolean option with the given configuration. */ public static Option bool(String prefix, String name, Enum category, String description) { return new BooleanOption(prefix, name, category, null, description); } /** * Create a new Boolean option with the given configuration. */ public static Option bool(String longName, Enum category, String description) { return new BooleanOption(longName, category, null, description); } /** * Create a new Boolean option with the given configuration. */ public static Option bool(String prefix, String name, Enum category, Boolean defval, String description) { return new BooleanOption(prefix, name, category, defval, description); } /** * Create a new Boolean option with the given configuration. */ public static Option bool(String longName, Enum category, Boolean defval, String description) { return new BooleanOption(longName, category, defval, description); } /** * Create a new Integer option with the given configuration. */ public static Option integer(String prefix, String name, Enum category, String description) { return new IntegerOption(prefix, name, category, null, description); } /** * Create a new Integer option with the given configuration. */ public static Option integer(String prefix, String name, Enum category, Integer[] options, String description) { return new IntegerOption(prefix, name, category, options, null, description); } /** * Create a new Integer option with the given configuration. */ public static Option integer(String longName, Enum category, String description) { return new IntegerOption(longName, category, null, description); } /** * Create a new Integer option with the given configuration. */ public static Option integer(String longName, Enum category, Integer[] options, String description) { return new IntegerOption(longName, category, options, null, description); } /** * Create a new Integer option with the given configuration. */ public static Option integer(String prefix, String name, Enum category, Integer defval, String description) { return new IntegerOption(prefix, name, category, defval, description); } /** * Create a new Integer option with the given configuration. */ public static Option integer(String longName, Enum category, Integer defval, String description) { return new IntegerOption(longName, category, defval, description); } /** * Create a new Integer option with the given configuration. */ public static Option integer(String prefix, String name, Enum category, Integer[] options, Integer defval, String description) { return new IntegerOption(prefix, name, category, options, defval, description); } /** * Create a new Integer option with the given configuration. */ public static Option integer(String longName, Enum category, Integer[] options, Integer defval, String description) { return new IntegerOption(longName, category, options, defval, description); } /** * Create a new Enumeration-based option with the given configuration. */ public static > Option enumeration(String prefix, String name, Enum category, Class enumClass, String description) { return new EnumerationOption(prefix, name, category, enumClass, null, description); } /** * Create a new Enumeration-based option with the given configuration. */ public static > Option enumeration(String longName, Enum category, Class enumClass, String description) { return new EnumerationOption(longName, category, enumClass, null, description); } /** * Create a new Enumeration-based option with the given configuration. */ public static > Option enumeration(String prefix, String name, Enum category, T defval, String description) { return new EnumerationOption(prefix, name, category, defval.getClass(), defval, description); } /** * Create a new Enumeration-based option with the given configuration. */ public static > Option enumeration(String longName, Enum category, T defval, String description) { return new EnumerationOption(longName, category, defval.getClass(), defval, description); } /** * Format the given options to show their loaded values in the current JVM. */ public static String formatValues(Option... options) { return formatValues(Arrays.asList(options)); } /** * Format the given options to show their loaded values in the current JVM. */ public static String formatValues(Collection