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

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

The newest version!
/*******************************************************************************
 * Copyright (c) 2003, 2010 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:  $
package at.spardat.xma.mdl.util;

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

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 numeric value and adds Transactional and XMA-Synchronization behavior.
 * It can be used for byte, short, int and long.
 * The desired type is given to the constructor.
 * The type is used during synchronization.
 *
 * @author gub
 * @since 2.3.0
 */
public final class TransNumber implements Transactional, Synchronization, Descriptive {
    /** treat the value as byte */
    public static final byte T_BYTE=1;
    /** treat the value as short */
    public static final byte T_SHORT=2;
    /** treat the value as int */
    public static final byte T_INT=3;
    /** treat the value as long */
    public static final byte T_LONG=4;

    /** logical type of value */
    byte type;
    /** the current value */
    long value;
    /** the value at the last syncpoint. May only be accessed if hasHistory is true.*/
    long saved;
    /** indicates that saved contains a saved state */
    boolean hasHistory=false;

    /**
     * Constructs a TransNumber.
     * @param type the logical type of the value
     * @param value the initial value
     */
    public TransNumber(byte type,long value) {
        this.type=type;
        this.value=value;
    }

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

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.Transactional#commit()
     */
    public void commit() {
        saved=0;
        hasHistory=false;
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.Transactional#rollback()
     */
    public void rollback() {
        if(hasHistory) {
            value=saved;
            hasHistory=false;
        }
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.Synchronization#externalize(XmaOutput,boolean)
     */
    public void externalize(XmaOutput xo, boolean forceFull) throws IOException {
        switch(type) {
            case T_BYTE: xo.writeByte("byte", (byte)value); break;
            case T_SHORT: xo.writeShort("short", (short)value); break;
            case T_INT: xo.writeInt("short", (int)value); break;
            case T_LONG: xo.writeLong("short", (long)value); break;
        }
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.Synchronization#internalize(XmaInput)
     */
    public void internalize(XmaInput in, List formattable) throws IOException {
        switch(type) {
            case T_BYTE: value =in.readByte(); break;
            case T_SHORT: value =in.readShort(); break;
            case T_INT: value =in.readInt(); break;
            case T_LONG: value =in.readLong(); break;
        }
        saved=0;
        hasHistory=false;
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.mdl.Descriptive#describe(DNode)
     */
    public void describe(DNode node) {
        node.sb();
        node.app("type", Byte.toString(type)).comma()
            .app("value", Long.toString(value)).comma()
            .app("saved", Long.toString(saved)).comma()
            .app("changed", hasHistory);
        node.eb();
    }

    /**
     * Set the current value
     * @throws IllegalArgumentException if the new value does not fit into the logical type
     */
    public void set(long value) {
        switch(type) {
            case T_BYTE: if(value>Byte.MAX_VALUE||valueShort.MAX_VALUE||valueInteger.MAX_VALUE||value




© 2015 - 2024 Weber Informatics LLC | Privacy Policy