Examples

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

Example GapList

// GapList replaces all ArrayList, LinkedList, ArrayDeque
List 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 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 list = new KeyList.Builder().withNull(false).build();

Example KeyListWithConstraint

// A list with a user defined constraint
IPredicate uppercasePredicate = new IPredicate() {
    public boolean test(String elem) {
        return elem.equals(elem.toUpperCase());
    }
};
KeyList list = new KeyList.Builder().withConstraint(uppercasePredicate).build();

// Java 8
list = new KeyList.Builder().withConstraint(s -> s.equals(s.toUpperCase())).build();

Example KeyListWithMaxSize

// A list which can store a maximum of 5 elements
KeyList list = new KeyList.Builder().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 list = new KeyList.Builder().withWindowSize(5).build();

Example KeyListWithTrigger

KeyList list = new KeyList.Builder().
    withBeforeInsertTrigger(new IConsumer() {
        public void accept(String elem) {
            System.out.println("insert: " + elem);
        }
    }).
    withBeforeDeleteTrigger(new IConsumer() {
        public void accept(String elem) {
            System.out.println("delete: " + elem);
        }
    }).build();

// Java 8
list = new KeyList.Builder().
    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 list = new KeyList.Builder().withElemSet().build();

Example KeyListWithElemNull

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

Example KeyListWithElemDuplicates

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

Example KeyListWithPrimaryElem

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

Example KeyListWithUniqueElem

// List with set for fast access to elements (no duplicate values allowed except null)
KeyList list = new KeyList.Builder().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 list = new KeyList.Builder().withElemSort(true).build();

// Sort set by user defined comparator, null values are sorted last.
Comparator cmp = new Comparator() {
    public int compare(String s1, String s2) {
        return s1.toLowerCase().compareTo(s2.toLowerCase());
    }
};
list = new KeyList.Builder().withElemSort(cmp, false).build();

// Java 8
list = new KeyList.Builder().
    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 list = new KeyList.Builder().withOrderByElem(true).build();
// Sort element set and list by specified comparator, nulls last
Comparator comparator = new ReverseComparator(NaturalComparator.INSTANCE(String.class));
list = new KeyList.Builder().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 list = new KeyList.Builder().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
IFunction nameMapper = new IFunction() {
    public String apply(Column column) {
        return column.getName();
    }
};
Key1List list = new Key1List.Builder().
        withPrimaryKey1Map(nameMapper).build();

// Java 8
list = new Key1List.Builder().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.
IPredicate ticketConstraint = new IPredicate() {
    public boolean test(Ticket ticket) {
        return ticket.getText() != null;
    }
};
IFunction idMapper = new IFunction() {
    public String apply(Ticket ticket) {
        return ticket.getId();
    }
};
IFunction extIdMapper = new IFunction() {
    public String apply(Ticket ticket) {
        return ticket.getExtId();
    }
};
Key2List list = new Key2List.Builder().
        withConstraint(ticketConstraint).
        withPrimaryKey1Map(idMapper).
        withUniqueKey2Map(extIdMapper).
        build();

// Java 8
  list = new Key2List.Builder().
          withConstraint(t -> t.getText()!=null).
          withPrimaryKey1Map(Ticket::getId).
          withUniqueKey2Map(Ticket::getExtId).
          build();

Example KeyCollection

// A KeyCollection will always have element set automatically added
KeyCollection coll = new KeyCollection.Builder().build();
// So these two declarations are equal
coll = new KeyCollection.Builder().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 coll = new KeyCollection.Builder().withElemCount(true).build();

Example Key1Collection

// Use a Key1Collection store tag elements with a name as primary key.
IFunction nameMapper = new IFunction() {
    public String apply(Tag tag) {
        return tag.getName();
    }
};
Key1Collection coll = new Key1Collection.Builder().
        withPrimaryKey1Map(nameMapper).build();

// Java 8
coll = new Key1Collection.Builder().withPrimaryKey1Map(Tag::getName).build();

Example Key2Collection

// Use a Key2Collection to construct a bidirectional map
// where both key sets are sorted.

IFunction mapper1 = new IFunction() {
    @Override
    public Integer apply(ZipCity entry) {
        return entry.getZip();
    }
};

Key2Collection zipCities = new Key2Collection.Builder().
    withPrimaryKey1Map(mapper1).withKey1Sort(true).
    withKey2Map(new IFunction() {
        @Override
        public String apply(ZipCity entry) {
            return entry.getCity();
        }
    }).
    withKey2Null(false).withKey2Sort(true).build();

// Java 8
  zipCities = new Key2Collection.Builder().
          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 allZips = zipCities.asMap1().keySet();
Set allCities = zipCities.asMap2().keySet();

String city = zipCities.getByKey1(5000).getCity();
List zips = zipCities.getAllByKey2("Aarau").mappedList(zipCities.getKey1Mapper());

Example AsSet

KeyList list = new KeyList.Builder().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 set = list.asSet();

Example AsMap

IFunction nameMapper = new IFunction() {
    public String apply(Tag tag) {
        return tag.getName();
    }
};
Key1Collection coll = new Key1Collection.Builder().
        withKey1Map(nameMapper).build();

// Java 8
coll = new Key1Collection.Builder().withKey1Map(Tag::getName).build();
// Returns a modifiable map view of the collection.
Map 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;
    }
    public String toString() {
        return "BiEntry [zip=" + zip + ", city=" + city + "]";
    }
}

 
© Magicwerk.org