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

edu.vt.middleware.ldap.bean.AbstractLdapResult Maven / Gradle / Ivy

There is a newer version: 3.3.9
Show newest version
/*
  $Id: AbstractLdapResult.java 1330 2010-05-23 22:10:53Z dfisher $

  Copyright (C) 2003-2010 Virginia Tech.
  All rights reserved.

  SEE LICENSE FOR MORE INFORMATION

  Author:  Middleware Services
  Email:   [email protected]
  Version: $Revision: 1330 $
  Updated: $Date: 2010-05-23 18:10:53 -0400 (Sun, 23 May 2010) $
*/
package edu.vt.middleware.ldap.bean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchResult;

/**
 * AbstractLdapResult provides a base implementation of 
 * LdapResult where the underlying entries are backed by a 
 * Map.
 *
 * @param    type of backing map
 *
 * @author  Middleware Services
 * @version  $Revision: 1330 $ $Date: 2010-05-23 18:10:53 -0400 (Sun, 23 May 2010) $
 */
public abstract class AbstractLdapResult>
  extends AbstractLdapBean implements LdapResult
{

  /** hash code seed. */
  protected static final int HASH_CODE_SEED = 44;

  /** Entries contained in this result. */
  protected T entries;


  /**
   * Creates a new AbstractLdapResult with the supplied ldap bean
   * factory.
   *
   * @param  lbf  LdapBeanFactory
   */
  public AbstractLdapResult(final LdapBeanFactory lbf)
  {
    super(lbf);
  }


  /** {@inheritDoc} */
  public Collection getEntries()
  {
    return this.entries.values();
  }


  /** {@inheritDoc} */
  public LdapEntry getEntry(final String dn)
  {
    return this.entries.get(dn);
  }


  /** {@inheritDoc} */
  public void addEntry(final LdapEntry e)
  {
    this.entries.put(e.getDn(), e);
  }


  /** {@inheritDoc} */
  public void addEntry(final SearchResult sr)
    throws NamingException
  {
    final LdapEntry le = this.beanFactory.newLdapEntry();
    le.setEntry(sr);
    this.addEntry(le);
  }


  /** {@inheritDoc} */
  public void addEntries(final Collection c)
  {
    for (LdapEntry e : c) {
      this.entries.put(e.getDn(), e);
    }
  }


  /** {@inheritDoc} */
  public void addEntries(final NamingEnumeration ne)
    throws NamingException
  {
    while (ne.hasMore()) {
      final LdapEntry le = this.beanFactory.newLdapEntry();
      le.setEntry(ne.next());
      this.addEntry(le);
    }
  }


  /** {@inheritDoc} */
  public void addEntries(final Iterator i)
    throws NamingException
  {
    while (i.hasNext()) {
      final LdapEntry le = this.beanFactory.newLdapEntry();
      le.setEntry(i.next());
      this.addEntry(le);
    }
  }


  /** {@inheritDoc} */
  public int size()
  {
    return this.entries.size();
  }


  /** {@inheritDoc} */
  public void clear()
  {
    this.entries.clear();
  }


  /** {@inheritDoc} */
  public int hashCode()
  {
    int hc = HASH_CODE_SEED;
    for (LdapEntry e : this.entries.values()) {
      if (e != null) {
        hc += e.hashCode();
      }
    }
    return hc;
  }


  /**
   * This returns a string representation of this object.
   *
   * @return  String
   */
  @Override
  public String toString()
  {
    return String.format("%s", this.entries.values());
  }


  /** {@inheritDoc} */
  public List toSearchResults()
  {
    final List results = new ArrayList(
      this.entries.size());
    for (LdapEntry e : this.entries.values()) {
      results.add(e.toSearchResult());
    }
    return results;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy