All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.relogiclabs.json.schema.internal.collection.IndexHashMap Maven / Gradle / Ivy

Go to download

The New JSON Schema prioritizes simplicity, conciseness, and readability, making it user-friendly and accessible without the need for extensive prior knowledge. It offers efficient read-write facilities, precise JSON document definition through various data types and functions, and extensibility to meet modern web service diverse requirements.

There is a newer version: 1.12.1
Show newest version
package com.relogiclabs.json.schema.internal.collection;

import com.relogiclabs.json.schema.collection.IndexMap;
import com.relogiclabs.json.schema.collection.Keyable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class IndexHashMap> implements IndexMap {

    private boolean unmodifiable;
    private Map map;
    private List list;

    public IndexHashMap(Collection c) {
        map = new HashMap<>();
        list = new ArrayList<>();
        addAll(c);
    }

    @Override
    public TV get(int index) {
        return list.get(index);
    }

    @Override
    public TV get(TK key) {
        return map.get(key);
    }

    @Override
    public Set keySet() {
        return map.keySet();
    }

    @Override
    public Collection values() {
        return list;
    }

    @Override
    public boolean isUnmodifiable() {
        return unmodifiable;
    }

    @Override
    public IndexHashMap asUnmodifiable() {
        if(unmodifiable) return this;
        list = Collections.unmodifiableList(list);
        map = Collections.unmodifiableMap(map);
        unmodifiable = true;
        return this;
    }

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return list.contains(o);
    }

    @Override
    public Iterator iterator() {
        return list.iterator();
    }

    @Override
    public Object[] toArray() {
        return list.toArray();
    }

    @Override
    public  T[] toArray(T[] a) {
        return list.toArray(a);
    }

    @Override
    public boolean add(TV value) {
        if(map.containsKey(value.getKey())) return false;
        list.add(value);
        map.put(value.getKey(), value);
        return true;
    }

    @Override
    public boolean remove(Object o) {
        if(o instanceof Keyable value) {
            boolean result = list.remove(value);
            if(result) map.remove(value.getKey());
            return result;
        }
        return false;
    }

    @Override
    public boolean containsAll(Collection c) {
        for(Object o : c) if(!contains(o)) return false;
        return true;
    }

    @Override
    public boolean addAll(Collection c) {
        boolean result = false;
        for(TV value : c) result |= add(value);
        return result;
    }

    @Override
    public boolean removeAll(Collection c) {
        boolean result = false;
        for(Object o : c) result |= remove(o);
        return result;
    }

    @Override
    public boolean retainAll(Collection c) {
        boolean result = false;
        for(TV value : list) if(!c.contains(value)) result |= remove(value);
        return result;
    }

    @Override
    public void clear() {
        list.clear();
        map.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy