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

at.spardat.xma.mdl.util.TransString Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

// @(#) $Id: TransString.java 10268 2013-01-11 10:34:57Z dschwarz $
package at.spardat.xma.mdl.util;

import java.io.IOException;
import java.util.List;

import at.spardat.xma.mdl.MemoryEstimator;
import at.spardat.xma.mdl.Synchronization;
import at.spardat.xma.mdl.Transactional;
import at.spardat.xma.serializer.XmaInput;
import at.spardat.xma.serializer.XmaOutput;

/**
 * Encapsulates a String and adds Transactional behaviour to it. The value
 * of the String may be retrieved using get. Upon default construction, 
 * an instance contains a null String.
 * 
 * @author YSD, 17.04.2003 22:22:07
 */
public final class TransString implements Transactional, Synchronization, Descriptive {

    /**
     * Default constructor. The contained value is null.
     */
    public TransString () {
    }
    
    /**
     * Constructs with a provided string. The effect of calling this
     * constructor is the same as using the default constructor and 
     * calling set(s) afterwards. This implies that changed()
     * may be true after this constructor finishes.
     * 
     * @param s the String to set
     */
    public TransString (String s) {
        set (s);
    }
    
    /**
     * This method sets a new value.
     * 
     * @param s the String to set; may be null
     */
    public void set (String s) {
        if (equ(s, value_)) return;
        if (hasHistory_) {
            if (equ(s, saved_)) {
                // the saved state equals the one to set now;
                // discard the change information
                hasHistory_ = false;
                saved_ = null;
            }
        } else {
            // no save state
            saved_ = value_;
            hasHistory_ = true;
        }
        // anyway, the newly selected is set to the provided key 
        value_ = s;
    }
    
    /**
     * Returns the value of this
     * 
     * @return a String that may be null
     */
    public String get () {
        return value_;
    }

    // checks if two strings are equal, including the treatment of null Strings.
    // two null Strings are considered equal. A null String and a non null String
    // are not equal.
    private static boolean equ (String s1, String s2) {
        if (s1 == null) return s2 == null;
        else return s1.equals(s2);
    }

    /**
     * @see at.spardat.xma.mdl.Transactional#changed()
     */
    public boolean changed () {
        return hasHistory_;
    }

    /**
     * @see at.spardat.xma.mdl.Transactional#rollback()
     */
    public void rollback () {
        if (hasHistory_) {
            value_ = saved_;
            hasHistory_ = false;
        }
    }

    /**
     * @see at.spardat.xma.mdl.Transactional#commit()
     */
    public void commit () {
        hasHistory_ = false;
        saved_ = null;  // release string
    }

    /**
     * @see at.spardat.xma.mdl.Synchronization#externalize(at.spardat.xma.serializer.XmaOutput, boolean)
     */
    public void externalize (XmaOutput xo, boolean forceFull) throws IOException {
        // write boolean to indicate if the value is null
        xo.writeBoolean("isNull", value_ == null);
        if (value_ != null) {
            xo.writeString("val", value_);
        }
    }

    /**
     * @see at.spardat.xma.mdl.Synchronization#internalize(at.spardat.xma.serializer.XmaInput, List)
     */
    public void internalize (XmaInput in, List formattable) throws IOException, ClassNotFoundException {
        boolean isNull = in.readBoolean();
        if (isNull) value_ = null;
        else {
            value_ = in.readString();
        }
        // discard change information
        hasHistory_ = false;
        saved_ = null;
    }
    
    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals (Object obj) {
        if (obj == null || !(obj instanceof TransString)) return false;
        return equ (value_, ((TransString)obj).value_);
    }

    
    /**
     * @see at.spardat.xma.mdl.util.Descriptive#describe(at.spardat.xma.mdl.util.DNode)
     */
    public void describe (DNode n) {
        n.sb();
        n.app("value", value_).comma().app("saved", saved_).comma().app("changed", hasHistory_);
        n.eb();
    }
    
    /**
     * Estimates the number of bytes this object consumes in memory.
     */
    public int estimateMemory () {
        return MemoryEstimator.sizeOfObject(3) + (value_ == null ? 0 : MemoryEstimator.sizeOf(value_));
    }
    
    
    
    /**
     * The actual value of this. May be null.
     */
    private String              value_;

    /**
     * The value at the last syncpoint. May only be accessed if hasHistory is true.
     */
    private String              saved_;
    
    // Indicates that saved_ contains a saved state.
    private boolean             hasHistory_ = false;
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy