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

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

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

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

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 MapCollectionHandler 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 HashMap();
      ((Map) collection).put(key, value);
      return collection;
    }
    ((Map) collection).put(key, value);
    return null;
  }

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy