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

org.exolab.castor.mapping.loader.collection.handler.SortedMapCollectionHandler Maven / Gradle / Ivy

package org.exolab.castor.mapping.loader.collection.handler;

import java.util.Enumeration;
import java.util.SortedMap;
import java.util.TreeMap;

import org.exolab.castor.mapping.CollectionHandler;
import org.exolab.castor.mapping.MapItem;
import org.exolab.castor.mapping.loader.CollectionHandlers;
import org.exolab.castor.mapping.loader.J2CollectionHandlers.IteratorEnumerator;

public final class SortedMapCollectionHandler implements CollectionHandler {

  @SuppressWarnings("unchecked")
  public Object add(Object collection, T object) {

    T key = object;
    T value = object;

    if (object instanceof MapItem) {
      MapItem item = (MapItem) object;
      key = item.getKey();
      value = item.getValue();
      if (value == null) {
        value = object;
      }
      if (key == null) {
        key = value;
      }
    }

    if (collection == null) {
      collection = new TreeMap();
      ((SortedMap) collection).put(key, value);
      return collection;
    }
    ((SortedMap) collection).put(key, value);
    return null;
  }

  @SuppressWarnings("unchecked")
  public Enumeration elements(final Object collection) {
    if (collection == null)
      return new CollectionHandlers.EmptyEnumerator();
    return new IteratorEnumerator(((SortedMap) collection).values().iterator());
  }

  @SuppressWarnings("unchecked")
  public int size(final Object collection) {
    if (collection == null)
      return 0;
    return ((SortedMap) collection).size();
  }

  @SuppressWarnings("unchecked")
  public Object clear(final Object collection) {
    if (collection != null)
      ((SortedMap) collection).clear();
    return null;
  }

  public String toString() {
    return "SortedMap";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy