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

net.sf.mmm.util.pojo.descriptor.base.PojoMap Maven / Gradle / Ivy

The newest version!
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.pojo.descriptor.base;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import net.sf.mmm.util.collection.base.AbstractSimpleMap;
import net.sf.mmm.util.pojo.descriptor.api.PojoDescriptor;
import net.sf.mmm.util.pojo.descriptor.api.PojoDescriptorBuilder;
import net.sf.mmm.util.pojo.descriptor.api.PojoPropertyDescriptor;
import net.sf.mmm.util.pojo.descriptor.api.accessor.PojoPropertyAccessorNonArgMode;

/**
 * This is represents a given {@link net.sf.mmm.util.pojo.api.Pojo} as {@link java.util.Map} where the key is
 * a {@link net.sf.mmm.util.pojo.descriptor.api.accessor.PojoPropertyAccessor#getName() property-name} given
 * as String.
 *
 * @author Joerg Hohwiller (hohwille at users.sourceforge.net)
 * @since 1.1.1
 */
public class PojoMap extends AbstractSimpleMap {

  @SuppressWarnings("rawtypes")
  private final PojoDescriptor pojoDescriptor;

  private final Object pojo;

  private Set keySet;

  /**
   * The constructor.
   *
   * @param pojoDescriptorBuilder is the {@link PojoDescriptorBuilder} to use.
   * @param pojo the {@link net.sf.mmm.util.pojo.api.Pojo} to represent as {@link java.util.Map}.
   */
  public PojoMap(PojoDescriptorBuilder pojoDescriptorBuilder, Object pojo) {

    super();
    this.pojoDescriptor = pojoDescriptorBuilder.getDescriptor(pojo.getClass());
    this.pojo = pojo;
    this.keySet = null;
  }

  @Override
  @SuppressWarnings("unchecked")
  public Object get(Object key) {

    String pojoPath = (String) key;
    return this.pojoDescriptor.getProperty(this.pojo, pojoPath);
  }

  @Override
  public Set keySet() {

    if (this.keySet == null) {
      Set keys = new HashSet<>();
      PojoDescriptor descriptor = this.pojoDescriptor;
      for (PojoPropertyDescriptor propertyDescriptor : descriptor.getPropertyDescriptors()) {
        if (propertyDescriptor.getAccessor(PojoPropertyAccessorNonArgMode.GET) != null) {
          keys.add(propertyDescriptor.getName());
        }
      }
      this.keySet = Collections.unmodifiableSet(keys);
    }
    return this.keySet;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy