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

net.sf.okapi.common.observer.BaseObservable Maven / Gradle / Ivy

There is a newer version: 1.47.0
Show newest version
/*
 Copyright (C) 2007 Richard Gomes

 This source code is release under the BSD License.
 
 This file is part of JQuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://jquantlib.org/

 JQuantLib is free software: you can redistribute it and/or modify it
 under the terms of the JQuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 . The license is also available online at
 .

 This program 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 license for more details.
 
 JQuantLib is based on QuantLib. http://quantlib.org/
 When applicable, the original copyright notice follows this notice.
 */
/*===========================================================================
  Additional changes:
  Copyright (C) 2008-2009 by the Okapi Framework contributors
-----------------------------------------------------------------------------
  Licensed 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 net.sf.okapi.common.observer;

import java.util.Collections; //FIXME: performance
import java.util.List; //FIXME: performance
import java.util.concurrent.CopyOnWriteArrayList;

// --------------------------------------------------------
// This class is based on the work done by Martin Fischer.
// See references in JavaDoc
//--------------------------------------------------------

/**
 * Default implementation of an {@link IObservable}.
 * 

* This implementation notifies the observers in a synchronous fashion. Note * that this can cause trouble if you notify the observers while in a * transactional context because once the notification is done it cannot be * rolled back. * * @see * Martin Fischer: Observer and Observable interfaces * @see Improved * Observer/Observable * * @see IObservable * @see IObserver * @see WeakReferenceObservable * * @author Richard Gomes * @author Srinivas Hasti */ public class BaseObservable implements IObservable { // // private final fields // private final List observers = new CopyOnWriteArrayList<>(); private final IObservable observable; // // public constructors // public BaseObservable(IObservable observable) { if (observable == null) throw new NullPointerException("observable is null"); this.observable = observable; } // // public methods // public void addObserver(final IObserver observer) { if (observer == null) throw new NullPointerException("observer is null"); observers.add(observer); } public int countObservers() { return observers.size(); } public List getObservers() { return Collections.unmodifiableList(this.observers); } public void deleteObserver(final IObserver observer) { observers.remove(observer); } public void deleteObservers() { observers.clear(); } public void notifyObservers() { notifyObservers(null); } public void notifyObservers(Object arg) { for (IObserver observer : observers) { wrappedNotify(observer, observable, arg); } } // // protected methods // /** * This method is intended to encapsulate the notification semantics, in * order to let extended classes to implement their own version. Possible * implementations are:

  • remote notification;
  • notification via * SwingUtilities.invokeLater
  • others...
* *

* The default notification simply does * *

	 * observer.update(observable, arg);
	 * 
* * @param observer the observer. * @param observable the object to be observed. * @param arg additional arguments. */ protected void wrappedNotify(IObserver observer, IObservable observable, Object arg) { observer.update(observable, arg); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy