Rambler's Top100
/*
 *  Copyright (C) 2004 Roman Krylov
 *  rkrylov@mail.ru
 *  
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  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
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package org.JWrapper;

public abstract class JLeafWrapper extends JWrapper
{
    protected java.util.Vector > validators
            = new java.util.Vector >();
    //Do we need validators at all if we have exceptions???
    
    protected Signal0 changed = new Signal0();
    protected JAccessAdapter access_adapter;//subclasses shouldn't use it directly!
    
    public JLeafWrapper(String _name, JAccessAdapter _aa)
        {
        super(_name);
        access_adapter = _aa;
        }
    
    public synchronized final void refresh() throws Exception
        {
        this.getValue();
        if(new_value!=previous_value)
            { changed.Op(); }
        }
    
    public final JAccessAdapter getAccessAdapter()
        { return access_adapter; }
    public final void setAccessAdapter(JAccessAdapter aa)
        { access_adapter=aa; }
    
    public final Signal0 getSignalChanged()
        { return changed; }
    
    public abstract String getValue();
    
    protected final void validate(T t) throws Exception//throws on denial
        {
        for(function1 validator : validators)
            { validator.Op(t); }
        }
    
    public final void setValue(String str) throws Exception
        {
        T value = null;
        try{
            value = this.str2value(str);
            setRawValue(value);
            }
            catch(Exception ex) { refresh(); throw ex; }
        }
    /* setValue(T arg) is protected to not to pollute
     * JLeafWrapper's subclasses
     */
    public final void setRawValue(T value) throws Exception
        {
        validate(value);
        access_adapter.set(value);
        changed.Op();
        }
            protected T
                previous_value = null,
                new_value = null;
    public final T getRawValue() throws Exception
        {
        previous_value = new_value;
        new_value = access_adapter.get();
        return new_value;
        }
            
    //template method:(could be a mutator!!!)
    protected abstract T str2value(String str) throws Exception;
    
    public String toString()
        { return super.toString()+": "+this.getValue(); }

}

Предприятие реального времени №1

На главную   Новости   Карта сайта
  Rambler's Top100