Search in sources :

Example 1 with CodeListSet

use of org.apache.sis.util.collection.CodeListSet in project sis by apache.

the class Freezer method clone.

/**
 * Returns an unmodifiable copy of the specified object.
 * This method performs the following heuristic tests:
 *
 * <ul>
 *   <li>If the specified object is an instance of {@code ModifiableMetadata},
 *       then {@link ModifiableMetadata#unmodifiable()} is invoked on that object.</li>
 *   <li>Otherwise, if the object is a {@linkplain Collection collection}, then the
 *       content is copied into a new collection of similar type, with values replaced
 *       by their unmodifiable variant.</li>
 *   <li>Otherwise, if the object implements the {@link Cloneable} interface,
 *       then a clone is returned.</li>
 *   <li>Otherwise, the object is assumed immutable and returned unchanged.</li>
 * </ul>
 *
 * @param  object  the object to convert in an immutable one.
 * @return a presumed immutable view of the specified object.
 */
@Override
public Object clone(final Object object) throws CloneNotSupportedException {
    /*
         * CASE 1 - The object is an org.apache.sis.metadata.* implementation. It may have
         *          its own algorithm for creating an unmodifiable view of metadata.
         */
    if (object instanceof ModifiableMetadata) {
        return ((ModifiableMetadata) object).unmodifiable();
    }
    if (object instanceof DefaultRepresentativeFraction) {
        final DefaultRepresentativeFraction c = ((DefaultRepresentativeFraction) object).clone();
        c.freeze();
        return c;
    }
    /*
         * CASE 2 - The object is a collection. All elements are replaced by their
         *          unmodifiable variant and stored in a new collection of similar
         *          type.
         */
    if (object instanceof Collection<?>) {
        Collection<?> collection = (Collection<?>) object;
        final boolean isSet = (collection instanceof Set<?>);
        final Object[] array = collection.toArray();
        switch(array.length) {
            case 0:
                {
                    collection = isSet ? Collections.EMPTY_SET : Collections.EMPTY_LIST;
                    break;
                }
            case 1:
                {
                    final Object value = clone(array[0]);
                    collection = isSet ? Collections.singleton(value) : Collections.singletonList(value);
                    break;
                }
            default:
                {
                    if (isSet) {
                        if (collection instanceof EnumSet<?>) {
                            collection = Collections.unmodifiableSet(((EnumSet<?>) collection).clone());
                        } else if (collection instanceof CodeListSet<?>) {
                            collection = Collections.unmodifiableSet(((CodeListSet<?>) collection).clone());
                        } else {
                            clones(array);
                            collection = CollectionsExt.immutableSet(false, array);
                        }
                    } else {
                        /*
                         * Do not use the SIS Checked* classes since we don't need type checking anymore.
                         * Conservatively assumes a List if we are not sure to have a Set since the list
                         * is less destructive (no removal of duplicated values).
                         */
                        clones(array);
                        collection = UnmodifiableArrayList.wrap(array);
                    }
                    break;
                }
        }
        return collection;
    }
    /*
         * CASE 3 - The object is a map. Copies all entries in a new map and replaces all values
         *          by their unmodifiable variant. The keys are assumed already immutable.
         */
    if (object instanceof Map<?, ?>) {
        final Map<Object, Object> map = new LinkedHashMap<>((Map<?, ?>) object);
        for (final Map.Entry<Object, Object> entry : map.entrySet()) {
            entry.setValue(clone(entry.getValue()));
        }
        return CollectionsExt.unmodifiableOrCopy(map);
    }
    /*
         * CASE 4 - The object is presumed cloneable.
         */
    if (object instanceof Cloneable) {
        return super.clone(object);
    }
    /*
         * CASE 5 - Any other case. The object is assumed immutable and returned unchanged.
         */
    return object;
}
Also used : CodeListSet(org.apache.sis.util.collection.CodeListSet) DefaultRepresentativeFraction(org.apache.sis.metadata.iso.identification.DefaultRepresentativeFraction) LinkedHashMap(java.util.LinkedHashMap) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with CodeListSet

use of org.apache.sis.util.collection.CodeListSet in project tika by apache.

the class GeographicInformationParser method getMetaDataIdentificationInfo.

private void getMetaDataIdentificationInfo(Metadata metadata, DefaultMetadata defaultMetaData) {
    ArrayList<Identification> identifications = (ArrayList<Identification>) defaultMetaData.getIdentificationInfo();
    for (Identification i : identifications) {
        DefaultDataIdentification defaultDataIdentification = (DefaultDataIdentification) i;
        if (i.getCitation() != null && i.getCitation().getTitle() != null)
            metadata.add("IdentificationInfoCitationTitle ", i.getCitation().getTitle().toString());
        ArrayList<CitationDate> dateArrayList = (ArrayList<CitationDate>) i.getCitation().getDates();
        for (CitationDate d : dateArrayList) {
            if (d.getDateType() != null)
                metadata.add("CitationDate ", d.getDateType().name() + "-->" + d.getDate());
        }
        ArrayList<ResponsibleParty> responsiblePartyArrayList = (ArrayList<ResponsibleParty>) i.getCitation().getCitedResponsibleParties();
        for (ResponsibleParty r : responsiblePartyArrayList) {
            if (r.getRole() != null)
                metadata.add("CitedResponsiblePartyRole ", r.getRole().toString());
            if (r.getIndividualName() != null)
                metadata.add("CitedResponsiblePartyName ", r.getIndividualName().toString());
            if (r.getOrganisationName() != null)
                metadata.add("CitedResponsiblePartyOrganizationName ", r.getOrganisationName().toString());
            if (r.getPositionName() != null)
                metadata.add("CitedResponsiblePartyPositionName ", r.getPositionName().toString());
            if (r.getContactInfo() != null) {
                for (String s : r.getContactInfo().getAddress().getElectronicMailAddresses()) {
                    metadata.add("CitedResponsiblePartyEMail ", s.toString());
                }
            }
        }
        if (i.getAbstract() != null)
            metadata.add("IdentificationInfoAbstract ", i.getAbstract().toString());
        for (Progress p : i.getStatus()) {
            metadata.add("IdentificationInfoStatus ", p.name());
        }
        ArrayList<Format> formatArrayList = (ArrayList<Format>) i.getResourceFormats();
        for (Format f : formatArrayList) {
            if (f.getName() != null)
                metadata.add("ResourceFormatSpecificationAlternativeTitle ", f.getName().toString());
        }
        CheckedHashSet<Locale> localeCheckedHashSet = (CheckedHashSet<Locale>) defaultDataIdentification.getLanguages();
        for (Locale l : localeCheckedHashSet) {
            metadata.add("IdentificationInfoLanguage-->", l.getDisplayLanguage(Locale.ENGLISH));
        }
        CodeListSet<TopicCategory> categoryList = (CodeListSet<TopicCategory>) defaultDataIdentification.getTopicCategories();
        for (TopicCategory t : categoryList) {
            metadata.add("IdentificationInfoTopicCategory-->", t.name());
        }
        ArrayList<Keywords> keywordList = (ArrayList<Keywords>) i.getDescriptiveKeywords();
        int j = 1;
        for (Keywords k : keywordList) {
            j++;
            ArrayList<InternationalString> stringList = (ArrayList<InternationalString>) k.getKeywords();
            for (InternationalString s : stringList) {
                metadata.add("Keywords " + j, s.toString());
            }
            if (k.getType() != null)
                metadata.add("KeywordsType " + j, k.getType().name());
            if (k.getThesaurusName() != null && k.getThesaurusName().getTitle() != null)
                metadata.add("ThesaurusNameTitle " + j, k.getThesaurusName().getTitle().toString());
            if (k.getThesaurusName() != null && k.getThesaurusName().getAlternateTitles() != null)
                metadata.add("ThesaurusNameAlternativeTitle " + j, k.getThesaurusName().getAlternateTitles().toString());
            ArrayList<CitationDate> citationDates = (ArrayList<CitationDate>) k.getThesaurusName().getDates();
            for (CitationDate cd : citationDates) {
                if (cd.getDateType() != null)
                    metadata.add("ThesaurusNameDate ", cd.getDateType().name() + "-->" + cd.getDate());
            }
        }
        ArrayList<DefaultLegalConstraints> constraintList = (ArrayList<DefaultLegalConstraints>) i.getResourceConstraints();
        for (DefaultLegalConstraints c : constraintList) {
            for (Restriction r : c.getAccessConstraints()) {
                metadata.add("AccessContraints ", r.name());
            }
            for (InternationalString s : c.getOtherConstraints()) {
                metadata.add("OtherConstraints ", s.toString());
            }
            for (Restriction r : c.getUseConstraints()) {
                metadata.add("UserConstraints ", r.name());
            }
        }
        Collection<Extent> extentList = ((DefaultDataIdentification) i).getExtents();
        for (Extent e : extentList) {
            ArrayList<GeographicExtent> geoElements = (ArrayList<GeographicExtent>) e.getGeographicElements();
            for (GeographicExtent g : geoElements) {
                if (g instanceof DefaultGeographicDescription) {
                    if (((DefaultGeographicDescription) g).getGeographicIdentifier() != null && ((DefaultGeographicDescription) g).getGeographicIdentifier().getCode() != null)
                        metadata.add("GeographicIdentifierCode ", ((DefaultGeographicDescription) g).getGeographicIdentifier().getCode().toString());
                    if (((DefaultGeographicDescription) g).getGeographicIdentifier() != null && ((DefaultGeographicDescription) g).getGeographicIdentifier().getAuthority() != null && ((DefaultGeographicDescription) g).getGeographicIdentifier().getAuthority().getTitle() != null)
                        metadata.add("GeographicIdentifierAuthorityTitle ", ((DefaultGeographicDescription) g).getGeographicIdentifier().getAuthority().getTitle().toString());
                    for (InternationalString s : ((DefaultGeographicDescription) g).getGeographicIdentifier().getAuthority().getAlternateTitles()) {
                        metadata.add("GeographicIdentifierAuthorityAlternativeTitle ", s.toString());
                    }
                    for (CitationDate cd : ((DefaultGeographicDescription) g).getGeographicIdentifier().getAuthority().getDates()) {
                        if (cd.getDateType() != null && cd.getDate() != null)
                            metadata.add("GeographicIdentifierAuthorityDate ", cd.getDateType().name() + " " + cd.getDate().toString());
                    }
                }
            }
        }
    }
}
Also used : Locale(java.util.Locale) CodeListSet(org.apache.sis.util.collection.CodeListSet) Keywords(org.opengis.metadata.identification.Keywords) GeographicExtent(org.opengis.metadata.extent.GeographicExtent) Extent(org.opengis.metadata.extent.Extent) CheckedArrayList(org.apache.sis.internal.util.CheckedArrayList) ArrayList(java.util.ArrayList) DefaultDataIdentification(org.apache.sis.metadata.iso.identification.DefaultDataIdentification) Identification(org.opengis.metadata.identification.Identification) InternationalString(org.opengis.util.InternationalString) Format(org.opengis.metadata.distribution.Format) DefaultLegalConstraints(org.apache.sis.metadata.iso.constraint.DefaultLegalConstraints) DefaultDataIdentification(org.apache.sis.metadata.iso.identification.DefaultDataIdentification) CitationDate(org.opengis.metadata.citation.CitationDate) Progress(org.opengis.metadata.identification.Progress) TopicCategory(org.opengis.metadata.identification.TopicCategory) ResponsibleParty(org.opengis.metadata.citation.ResponsibleParty) CheckedHashSet(org.apache.sis.internal.util.CheckedHashSet) GeographicExtent(org.opengis.metadata.extent.GeographicExtent) DefaultGeographicDescription(org.apache.sis.metadata.iso.extent.DefaultGeographicDescription) Restriction(org.opengis.metadata.constraint.Restriction) InternationalString(org.opengis.util.InternationalString)

Aggregations

CodeListSet (org.apache.sis.util.collection.CodeListSet)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1 CheckedArrayList (org.apache.sis.internal.util.CheckedArrayList)1 CheckedHashSet (org.apache.sis.internal.util.CheckedHashSet)1 DefaultLegalConstraints (org.apache.sis.metadata.iso.constraint.DefaultLegalConstraints)1 DefaultGeographicDescription (org.apache.sis.metadata.iso.extent.DefaultGeographicDescription)1 DefaultDataIdentification (org.apache.sis.metadata.iso.identification.DefaultDataIdentification)1 DefaultRepresentativeFraction (org.apache.sis.metadata.iso.identification.DefaultRepresentativeFraction)1 CitationDate (org.opengis.metadata.citation.CitationDate)1 ResponsibleParty (org.opengis.metadata.citation.ResponsibleParty)1 Restriction (org.opengis.metadata.constraint.Restriction)1 Format (org.opengis.metadata.distribution.Format)1 Extent (org.opengis.metadata.extent.Extent)1 GeographicExtent (org.opengis.metadata.extent.GeographicExtent)1 Identification (org.opengis.metadata.identification.Identification)1 Keywords (org.opengis.metadata.identification.Keywords)1