There we go, bells & whistles included.

Cheers,

Peter.


/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.river.api.lookup;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import net.jini.lookup.entry.Address;
import net.jini.lookup.entry.Comment;
import net.jini.lookup.entry.Location;
import net.jini.lookup.entry.Name;
import net.jini.lookup.entry.ServiceInfo;
import net.jini.lookup.entry.Status;
import net.jini.lookup.entry.UIDescriptor;
import org.apache.river.impl.util.ConcurrentCollections;

/**
* A little builder utility class that creates an array of Entry classes to
* be used as a parameter for StreamServiceRegistrar.
*
* Note: This class is thread safe.
*
* The toString method will return a snapshot of the Entry classes contained at
* the time of the toString method call, the String returned will be a
* space delimited list of all Entry classes.
*
*
* Suggested by Dan Creswell.
* @author peter
*/
public class DefaultEntries {
   private final Set<Class> entrys;
public DefaultEntries() {
       Set<Class> set = new HashSet<Class>(16);
       entrys = ConcurrentCollections.multiReadSet(set);
   }
   /**
    * Add an Entry class.
    * @param cl - class
    * @return this
    */
   public DefaultEntries add(Class cl){
       entrys.add(cl);
       return this;
   }
   /**
    * All all the Jini Platform Entry's
    * @return
    */
   public DefaultEntries addPlatformEntries(){
       add(Comment.class);
       add(Location.class);
       add(Name.class);
       add(ServiceInfo.class);
       add(Status.class);
       add(UIDescriptor.class);
       add(Address.class);
       return this;
   }
   /**
    * Remove all Entry's
    */
   public void reset(){
       entrys.clear();
   }
/**
    * Generate a new array containing all Entry's added since last reset.
    *
    * @return
    */
   public Class[] getEntries(){
       /* An empty array is passed in, it will be replaced by the correct
* size array. Checking the Set size first, then creating an array of
        * sufficient size would not be atomic and may lead to an array
        * filled with null's if reset was called between size and
        * passing the array into the entrys Set.
        */
       return entrys.toArray(new Class[0]);
   }

   @Override
   public int hashCode() {
       int hash = 3;
       hash = 29 * hash + this.entrys.hashCode();
       return hash;
   }

   @Override
   public boolean equals(Object o){
       if (o instanceof DefaultEntries){
           if (entrys.equals(((DefaultEntries)o).entrys)) return true;
       }
       return false;
   }
@Override
   public String toString(){
       StringBuilder sb = new StringBuilder(256);
       sb.append("DefaultEntries:");
       Iterator<Class> it = entrys.iterator();
       while (it.hasNext()){
           sb.append(" ");
           sb.append(it.next().getSimpleName());
       }
       return sb.toString();
   }
}


Reply via email to