开发者

How to store Java constants for use in building key value combinations

开发者 https://www.devze.com 2023-03-21 02:37 出处:网络
Let\'s say I want to store potential keys and potential values for those keys as constants.How can I achieve this?Or should I avoid it altogether?

Let's say I want to store potential keys and potential values for those keys as constants. How can I achieve this? Or should I avoid it altogether?

This is the aproach that I thought of myself, but as you'll be able to see, it has an obvious downfall.

public static class Foo { 

    public static final String KEY = "foo" ; 

    public stat开发者_Python百科ic class Values { 

        public static final String BAR = "bar" ; 
        public static final String HEY = "hey" ; 

    }

}

public static class Another { 

    public static final String KEY = "another" ; 

    public static class Values { 

        public static final String ONE = "1" ; 
        public static final String TWO = "two" ;
        public static final String THREE = "THREE" ;

    }

}

Which allows me to access these keys like so

miscellaneousMethod( Foo.KEY, Foo.Values.BAR )
miscellaneousMethod( Another.KEY, Another.Values.TWO )

However, I don't exactly want to write a separate static inner class for each key / possible-values pair.

Is there a better way to store key value pairs as constants?

I want to store them as constants for later comparison with generated hashmaps. So that I can ask stuff like this:

if( map.get( Foo.KEY ).equals( Foo.Values.HEY ) ) { /* do stuff */ }


If they are all constants, you might use an Enum:

public enum ValueEnum {

    FOO("foo", "bar", "hey"),
    ANOTHER("another", "1", "two", "THREE"),

    ;

    private final String key;

    private final Set<String> values;

    private ValueEnum(String key, String... values) {
        this.key = key;
        this.values = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(values)));
    }

    public final boolean isInMap(Map<String,String> map) {
        if(map.containsKey(key)) {
            return values.contains(map.get(key));
        }
        else {
            return false;
        }
    }
}

then

if( ValueEnum.FOO.isInMap(map) ) { /* do stuff */ }


Please avoid constants like that. For constants use the Java enum type. It compiles under the hood as classes so you get type safety, and you can also use them in switch statements. It's nice to be able to add methods to them too.

There's a nice example here: http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

A longer discussion (with many examples) is here: http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html


If possible, can you not just specify these keys in an XML file and use Java XML binding (JAXB) to load them.

  • you would avoid having to rebuild and deploy your code when and if the keys change.


Put the constants in a Map and then make it unmodifiable using the method Collections.unmodifiableMap().

0

精彩评论

暂无评论...
验证码 换一张
取 消