Examples

The classes used in the examples are listed at the end of the examples.

Example GapList

// GapList replaces all ArrayList, LinkedList, ArrayDeque
List<String> list = GapList.create("a", "b");

Example IntObjGapList

// IntObjGapList stores primitive int values instead of Objects,
// but allows access through the standard List and Dequeue interface
List<Integer> list = IntObjGapList.create(1, 2);
Integer value = list.get(0);

Example IntGapList

// IntGapList stores primitive int values instead of Objects
IntGapList list = IntGapList.create(1, 2);
int value = list.get(0);

Example KeyListWithNull

// A list with does not allow null values
KeyList<String> list = new KeyList.Builder<String>().withNull(false).build();

Example KeyListWithConstraint

// A list with a user defined constraint
KeyList<String> list = new KeyList.Builder<String>().withConstraint(s -> s.equals(s.toUpperCase())).build();

Example KeyListWithMaxSize

// A list which can store a maximum of 5 elements
KeyList<String> list = new KeyList.Builder<String>().withMaxSize(5).build();

Example KeyListWithWindowSize

// A list which can store a maximum of 5 elements.
// If an additional element is added, the first element is automatically removed first.
KeyList<String> list = new KeyList.Builder<String>().withWindowSize(5).build();

Example KeyListWithTrigger

KeyList<String> list = new KeyList.Builder<String>().withBeforeInsertTrigger(e -> System.out.println("before insert: " + e))
		.withAfterDeleteTrigger(e -> System.out.println("after delete: " + e)).build();

Example KeyListWithElemSet

// List with set for fast access to elements (all values allowed)
KeyList<String> list = new KeyList.Builder<String>().withElemSet().build();

Example KeyListWithElemNull

// List with set for fast access to elements (no null values allowed)
KeyList<String> list = new KeyList.Builder<String>().withElemNull(false).build();

Example KeyListWithElemDuplicates

// List with set for fast access to elements (no duplicate values allowed)
KeyList<String> list = new KeyList.Builder<String>().withElemDuplicates(false).build();

Example KeyListWithPrimaryElem

// List with set for fast access to elements (no duplicate or null values allowed)
KeyList<String> list = new KeyList.Builder<String>().withPrimaryElem().build();

Example KeyListWithUniqueElem

// List with set for fast access to elements (no duplicate values allowed except null)
KeyList<String> list = new KeyList.Builder<String>().withUniqueElem().build();

Example KeyListWithElemSort

// List with set for fast access to elements.
// The element set, but not the list is sorted by the natural comparator.
KeyList<String> list = new KeyList.Builder<String>().withElemSort(true).build();

// Sort set by user defined comparator, null values are sorted last.
list = new KeyList.Builder<String>().withElemSort((s1, s2) -> (s1.toLowerCase().compareTo(s2.toLowerCase())), false).build();

Example KeyListWithElemOrderBy

// List with set for fast access to elements.
// The element set and the list are sorted by the natural comparator.
KeyList<String> list = new KeyList.Builder<String>().withOrderByElem(true).build();

// Sort element set and list by specified comparator, nulls last
Comparator<String> comparator = NaturalComparator.INSTANCE(String.class).reversed();
list = new KeyList.Builder<String>().withElemSort(comparator, false).withOrderByElem(true).build();

Example KeyListWithElemOrderByClass

// List with set for fast access to elements.
// The set is realized as sorted list of primitive values.
KeyList<Integer> list = new KeyList.Builder<Integer>().withOrderByElem(int.class).build();

Example Key1List

// Use Key1List to implement a list of column names with a defined
// order and unique names which can be accessed fast
Key1List<Column, String> list = new Key1List.Builder<Column, String>().withPrimaryKey1Map(Column::getName).build();

Example Key2List

// Use Key2List to maintain a list of tickets.
// Each ticket has unique ID (primary key) and optional but also unique external ID.
Key2List<Ticket, String, String> list = new Key2List.Builder<Ticket, String, String>().withConstraint(t -> t.getText() != null)
		.withPrimaryKey1Map(Ticket::getId)
		.withUniqueKey2Map(Ticket::getExtId).build();

Example KeyCollection

// A KeyCollection implements java.util.Collection
KeyCollection<String> coll1 = new KeyCollection.Builder<String>().build();
// A KeyCollection will always have element set automatically added, so these two declarations are equal
KeyCollection<String> coll2 = new KeyCollection.Builder<String>().withElemSet().build();

Example KeyCollectionWithElemCount

// A KeyCollection which behaves like a multi set, i.e. it does not store duplicate elements,
// but only its number of occurrences.
KeyCollection<String> coll = new KeyCollection.Builder<String>().withElemCount(true).build();

Example Key1Collection

// Use a Key1Collection store tag elements with a name as primary key.
Key1Collection<Tag, String> coll = new Key1Collection.Builder<Tag, String>().withPrimaryKey1Map(Tag::getName).build();

Example Key2Collection

// Use a Key2Collection to construct a bidirectional map where both key sets are sorted.
Key2Collection<ZipCity, Integer, String> zipCities = new Key2Collection.Builder<ZipCity, Integer, String>().withPrimaryKey1Map(ZipCity::getZip)
		.withKey1Sort(true).withKey2Map(ZipCity::getCity)
		.withKey2Null(false).withKey2Sort(true).build();
zipCities.add(new ZipCity(4000, "Basel"));
zipCities.add(new ZipCity(5000, "Aarau"));
zipCities.add(new ZipCity(5001, "Aarau"));
zipCities.add(new ZipCity(6000, "Luzern"));

Set<Integer> allZips = zipCities.asMap1().keySet();
Set<String> allCities = zipCities.asMap2().keySet();

String city = zipCities.getByKey1(5000).getCity();
List<Integer> zips = GapList.create(zipCities.getAllByKey2("Aarau")).map(zipCities.getKey1Mapper());

Example KeySet

// A KeySet implements java.util.Set
KeySet<String> set = new KeySet.Builder<String>().build();

Example Key1Set

// Use a Key1Set store tag elements with a name as primary key.
Key1Set<Tag, String> coll = new Key1Set.Builder<Tag, String>().withPrimaryKey1Map(Tag::getName).build();

Example Key2Set

// Use a Key2Set to construct a bidirectional map where both key sets are sorted.
Key2Collection<ZipCity, Integer, String> zipCities = new Key2Set.Builder<ZipCity, Integer, String>().withPrimaryKey1Map(ZipCity::getZip)
		.withKey1Sort(true).withKey2Map(ZipCity::getCity)
		.withKey2Null(false).withKey2Sort(true).build();
zipCities.add(new ZipCity(4000, "Basel"));
zipCities.add(new ZipCity(5000, "Aarau"));
zipCities.add(new ZipCity(5001, "Aarau"));
zipCities.add(new ZipCity(6000, "Luzern"));

Set<Integer> allZips = zipCities.asMap1().keySet();
Set<String> allCities = zipCities.asMap2().keySet();

String city = zipCities.getByKey1(5000).getCity();
List<Integer> zips = GapList.create(zipCities.getAllByKey2("Aarau")).map(zipCities.getKey1Mapper());

Example AsSet

KeyList<String> list = new KeyList.Builder<String>().build();
// Returns a modifiable set view of the collection.
// Note that this method does not check whether the collection really is really a set
// as defined by the Set interface. It makes only sure that the add() method will return
// false instead of throwing a DuplicateKeyException.
Set<String> set = list.asSet();

Example AsMap

Key1Collection<Tag, String> coll = new Key1Collection.Builder<Tag, String>().withKey1Map(Tag::getName).build();
// Returns a modifiable map view of the collection.
Map<String, Tag> map = coll.asMap1();

Class Column

class Column {
	String name;
	String type;

	public Column(String name, String type) {
		this.name = name;
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public String getType() {
		return type;
	}
}

Class Tag

class Tag {
	String name;

	public Tag(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
}

Class Ticket

class Ticket {
	String id;
	String extId;
	String text;

	public Ticket(String id, String extId, String text) {
		this.id = id;
		this.extId = extId;
		this.text = text;
	}

	public String getId() {
		return id;
	}

	public String getExtId() {
		return extId;
	}

	public String getText() {
		return text;
	}

	@Override
	public String toString() {
		return "Ticket [id=" + id + ", extId=" + extId + ", text=" + text + "]";
	}
}

Class ZipCity

class ZipCity {
	int zip;
	String city;

	public ZipCity(int zip, String city) {
		this.zip = zip;
		this.city = city;
	}

	public int getZip() {
		return zip;
	}

	public String getCity() {
		return city;
	}

	@Override
	public String toString() {
		return "BiEntry [zip=" + zip + ", city=" + city + "]";
	}
}

 
© Magicwerk.org