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

com.cedarsoft.registry.DefaultRegistry Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) cedarsoft GmbH.
 *
 * Licensed under the GNU General Public License version 3 (the "License")
 * with Classpath Exception; you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *         http://www.cedarsoft.org/gpl3ce
 *         (GPL 3 with Classpath Exception)
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3 only, as
 * published by the Free Software Foundation. cedarsoft GmbH designates this
 * particular file as subject to the "Classpath" exception as provided
 * by cedarsoft GmbH in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 3 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 3 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact cedarsoft GmbH, 72810 Gomaringen, Germany,
 * or visit www.cedarsoft.com if you need additional information or
 * have any questions.
 */

package com.cedarsoft.registry;

import com.cedarsoft.commons.Converter;
import com.cedarsoft.exceptions.NotFoundException;
import com.cedarsoft.exceptions.StillContainedException;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 

DefaultRegistry class.

* * @param the type that is stored within this registry * @author Johannes Schneider ([email protected]) */ public class DefaultRegistry implements Registry { @Nonnull protected final List storedObjects = new ArrayList(); @Nonnull protected final ReadWriteLock lock = new ReentrantReadWriteLock(); /** * This comparator may optionally be set to ensure the registry only contains unique values */ @Nullable protected final Comparator comparator; /** * Creates an empty registry */ public DefaultRegistry() { comparator = null; } /** * Creates a registry with containing the given objects. * No comparator is set * * @param storedObjects the stored objects */ public DefaultRegistry( @Nonnull Collection storedObjects ) { this( storedObjects, null ); } /** * Creates an empty registry with the given (optional) comparator * * @param comparator the comparator */ public DefaultRegistry( @Nullable Comparator comparator ) { this.comparator = comparator; } /** * Creates a new registry * * @param storedObjects the initially stored objects * @param comparator the (optional) comparator */ public DefaultRegistry( @Nonnull Collection storedObjects, @Nullable Comparator comparator ) throws StillContainedException { this.comparator = comparator; //todo, really necessary??? if ( comparator != null ) { Collection set = new TreeSet( comparator ); set.addAll( storedObjects ); if ( storedObjects.size() != set.size() ) { throw new StillContainedException( "The stored objects collections contains duplicate entries" ); } } this.storedObjects.addAll( storedObjects ); } @Override @Nonnull public List getStoredObjects() { lock.readLock().lock(); try { return Collections.unmodifiableList( storedObjects ); } finally { lock.readLock().unlock(); } } @Override @Nullable public T findStoredObject( @Nonnull Matcher matcher ) { lock.readLock().lock(); try { for ( T object : storedObjects ) { if ( matcher.matches( object ) ) { return object; } } return null; } finally { lock.readLock().unlock(); } } /** *

findStoredObject

* * @param matcher a Matcher object. * @param notFoundMessage a String object. * @return a T object. */ @Override @Nonnull public T findStoredObject( @Nonnull Matcher matcher, @Nonnull String notFoundMessage ) throws NotFoundException { T found = findStoredObject( matcher ); if ( found == null ) { throw new NotFoundException( notFoundMessage ); } return found; } @Override @Nonnull public List findStoredObjects( @Nonnull Matcher matcher ) { lock.readLock().lock(); try { List found = new ArrayList(); for ( T object : storedObjects ) { if ( matcher.matches( object ) ) { found.add( object ); } } return found; } finally { lock.readLock().unlock(); } } @Override @Nonnull public List findStoredObjects( @Nonnull Matcher matcher, @Nonnull Converter converter ) { lock.readLock().lock(); try { List found = new ArrayList(); for ( T object : storedObjects ) { if ( matcher.matches( object ) ) { found.add( converter.convert( object ) ); } } return found; } finally { lock.readLock().unlock(); } } /** * {@inheritDoc} *

* Stores the object within the registry */ @Override public void store( @Nonnull T object ) throws StillContainedException { lock.writeLock().lock(); try { if ( comparator != null ) { for ( T storedObject : storedObjects ) { if ( comparator.compare( storedObject, object ) == 0 ) { throw new StillContainedException( object ); } } } storedObjects.add( object ); for ( Listener listener : new ArrayList>( listeners ) ) { listener.objectStored( object ); } } finally { lock.writeLock().unlock(); } } @Override public void updated( @Nonnull T object ) throws NotFoundException { lock.writeLock().lock(); try { for ( Listener listener : new ArrayList>( listeners ) ) { listener.objectUpdated( object ); } } finally { lock.writeLock().unlock(); } } @Override public void remove( @Nonnull T object ) throws NotFoundException { remove( object, "Cannot remove: Not found <" + object + ">" ); } @Override public void remove( @Nonnull T object, @Nonnull String removeMessage ) throws NotFoundException { lock.writeLock().lock(); try { if ( !storedObjects.remove( object ) ) { throw new NotFoundException( removeMessage ); } for ( Listener listener : new ArrayList>( listeners ) ) { listener.objectRemoved( object ); } } finally { lock.writeLock().unlock(); } } @Override @Nullable public Comparator getComparator() { return comparator; } @Override public boolean containsOnlyUniqueElements() { return comparator != null; } @Nonnull protected final List> listeners = new ArrayList>(); @Override public void addListener( @Nonnull Listener listener ) { lock.writeLock().lock(); try { this.listeners.add( listener ); } finally { lock.writeLock().unlock(); } } @Override public void removeListener( @Nonnull Listener listener ) { lock.writeLock().lock(); try { this.listeners.remove( listener ); } finally { lock.writeLock().unlock(); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy