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

org.zanata.rest.dto.resource.ExtensionSet Maven / Gradle / Ivy

There is a newer version: 4.6.2
Show newest version
package org.zanata.rest.dto.resource;

import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.zanata.rest.dto.DTOUtil;
import org.zanata.rest.dto.ExtensionValue;

import javax.annotation.Nonnull;

public class ExtensionSet extends
        AbstractCollection implements Serializable {

    private static final long serialVersionUID = 1L;
    private @Nonnull Map, T> extensions = new LinkedHashMap<>();

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

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

    @Override
    public boolean add(T e) {
        this.extensions.put(e.getClass(), e);
        return true;
    };

    @SuppressWarnings("unchecked")
    public  E findByType(Class clz) {
        return (E) this.extensions.get(clz);
    }

    @Override
    public String toString() {
        return DTOUtil.toXML(this);
    }

    public  E findOrAddByType(Class clz) {
        E ext = findByType(clz);
        if (ext == null) {
            try {
                ext = clz.newInstance();
                add(ext);
            } catch (Throwable e) {
                throw new RuntimeException("unable to create instance", e);
            }
        }
        return ext;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || !(o instanceof ExtensionSet)) {
            return false;
        }
        ExtensionSet that = (ExtensionSet) o;
        return extensions.equals(that.extensions);
    }

    @Override
    public int hashCode() {
        return extensions.hashCode();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy