org.aeonbits.owner.util.Collections Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of owner Show documentation
Show all versions of owner Show documentation
Get rid of the boilerplate code in Java properties based configuration.
/*
* Copyright (c) 2012-2015, Luigi R. Viggiano
* All rights reserved.
*
* This software is distributable under the BSD license.
* See the terms of the BSD license in the documentation provided with this software.
*/
package org.aeonbits.owner.util;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import static java.util.Arrays.asList;
/**
* Utility class to create a maps, lists and sets
* Examples of usage:
*
* import static org.aeonbits.owner.util.Collections.map;
* import static org.aeonbits.owner.util.Collections.entry;
* import static org.aeonbits.owner.util.Collections.set;
* import static org.aeonbits.owner.util.Collections.list;
*
* Map<String, String> myMap = map("foo", "bar");
* Map<String, String> myMap2 = map(entry("foo", "bar"), entry("baz", "qux");
* Set<String> mySet = set("foo", "bar", "baz", "qux");
* List<String> myList = list("foo", "bar", "baz", "qux");
*
*
* @author Luigi R. Viggiano
* @since 1.0.6
*/
public abstract class Collections {
// Suppresses default constructor, ensuring non-instantiability.
private Collections() {}
private static final class EntryMap extends AbstractMap implements Serializable {
private static final long serialVersionUID = -789853606407653214L;
private final Set> entries;
private EntryMap(Entry extends K, ? extends V>... entries) {
this.entries = set(entries);
}
@SuppressWarnings("unchecked")
@Override
public Set> entrySet() {
return (Set) entries;
}
}
public static Entry entry(K key, V value) {
return new SimpleEntry(key, value);
}
@SuppressWarnings("unchecked")
public static Map map(K key, V value) {
return map(entry(key, value));
}
public static Map map(Map.Entry extends K, ? extends V>... entries) {
return new EntryMap(entries);
}
public static Set set(E... elements) {
return new LinkedHashSet(list(elements));
}
public static List list(E... elements) {
return asList(elements);
}
}