001package com.identityworksllc.iiq.common;
002
003import java.io.Serializable;
004import java.util.HashMap;
005import java.util.List;
006
007/**
008 * An extension of HashMap that can be produced by {@link MapTupleBuilder}. Values
009 * can be accessed using either {@link #get(Object)} or {@link #getAt(int)},
010 * referencing by index or by key.
011 *
012 * This class also implements the first six 'tuple' getters, such as getFirst,
013 * getSecond, etc, which are equivalent to calling getAt(0), getAt(1), etc.
014 *
015 */
016public final class MapTuple extends HashMap<String, Object> implements Serializable {
017    /**
018     * The list of keys, used to determine the values by index
019     */
020    private final List<String> keys;
021
022    MapTuple(List<String> keys) {
023        this.keys = keys;
024    }
025
026    public <T> T get(String key, Class<T> expectedClass) {
027        Object result = get(key);
028        if (result != null) {
029            return expectedClass.cast(result);
030        } else {
031            return null;
032        }
033    }
034
035    public Object getAt(int index) {
036        if (index > keys.size()) {
037            return null;
038        }
039        return get(keys.get(index));
040    }
041
042    public <T> T getAt(int index, Class<T> expectedClass) {
043        Object result = getAt(index);
044        if (result != null) {
045            return expectedClass.cast(getAt(index));
046        } else {
047            return null;
048        }
049    }
050
051    public Object getFifth() {
052        return getAt(4);
053    }
054
055    public Object getFirst() {
056        return getAt(0);
057    }
058
059    public Object getFourth() {
060        return getAt(3);
061    }
062
063    public Object getSecond() {
064        return getAt(1);
065    }
066
067    public Object getSixth() {
068        return getAt(5);
069    }
070
071    public Object getThird() {
072        return getAt(2);
073    }
074}