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

org.nakedobjects.nos.remote.command.marshal.DataStructure Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
package org.nakedobjects.nos.remote.command.marshal;

import java.util.Hashtable;
import java.util.Vector;

import org.nakedobjects.noa.reflect.NakedObjectField;
import org.nakedobjects.noa.spec.NakedObjectSpecification;


/**
 * Caches a sorted version of the fields for a specified NakedObjectSpecification. This is used to counteract
 * any differences in field ordering that the specification might have across different tiers
 */
class DataStructure {
    private final Hashtable cache;

    {
        cache = new Hashtable();
    }

    public NakedObjectField[] getFields(final NakedObjectSpecification specification) {
        NakedObjectField[] fields = (NakedObjectField[]) cache.get(specification);
        if (fields == null) {
            fields = loadFields(specification);
            cache.put(specification, fields);
        }
        return fields;
    }

    private NakedObjectField[] loadFields(final NakedObjectSpecification specification) {
        NakedObjectField[] originalFields = specification.getFields();
        Vector sorted = new Vector(originalFields.length);
        outer: for (int i = 0; i < originalFields.length; i++) {
            String fieldId = originalFields[i].getId();

            for (int j = 0; j < sorted.size(); j++) {
                NakedObjectField sortedElement = (NakedObjectField) sorted.elementAt(j);
                String sortedFieldId = sortedElement.getId();
                if (sortedFieldId.compareTo(fieldId) > 0) {
                    sorted.insertElementAt(originalFields[i], j);
                    continue outer;
                }
            }
            sorted.addElement(originalFields[i]);
        }

        NakedObjectField[] fields = new NakedObjectField[originalFields.length];
        sorted.copyInto(fields);

        return fields;
    }

}
// Copyright (c) Naked Objects Group Ltd.




© 2015 - 2025 Weber Informatics LLC | Privacy Policy