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

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

/*******************************************************************************
 * 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: TransStringSetN.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.mdl.util;

import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;

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

/**
 * A transactional string set that is not size restricted.
 * 
 * @author YSD, 10.04.2003 11:30:25
 */
public class TransStringSetN extends TransStringSet {

    /**
     * @see at.spardat.xma.mdl.util.TransStringSet#commit()
     */
    public void commit() {
        saved_ = null;
    }

    /**
     * @see at.spardat.xma.mdl.util.TransStringSet#rollback()
     */
    public void rollback() {
        if (saved_ != null) { 
            selected_ = saved_;
            saved_ = null;
        } 
    }

    /**
     * @see at.spardat.xma.mdl.util.TransStringSet#changed()
     */
    public boolean changed() {
        return saved_ != null;
    }

    /**
     * @see at.spardat.xma.mdl.util.TransStringSet#add(java.lang.String)
     */
    public boolean add (String key) {
        if (key == null) throw new IllegalArgumentException();
        if (selected_.contains(key)) return false;
        ensureSaved();
        selected_.add(key);
        return true;
    }

    /**
     * @see at.spardat.xma.mdl.util.TransStringSet#remove(java.lang.String)
     */
    public boolean remove (String key) {
        if (!selected_.contains(key)) return false;
        ensureSaved();
        selected_.remove(key);
        return true;
    }

    /**
     * @see at.spardat.xma.mdl.util.TransStringSet#clear()
     */
    public void clear () {
        if (selected_.size() == 0) return;
        ensureSaved();
        selected_.clear();
    }

    /**
     * @see at.spardat.xma.mdl.util.TransStringSet#size()
     */
    public int size() {
        return selected_.size();
    }

    /**
     * @see at.spardat.xma.mdl.util.TransStringSet#getSome()
     */
    public String getSome() {
        if (selected_.size() == 0) return null;
        Iterator iter = selected_.iterator();
        return (String) iter.next();
    }

    /**
     * @see at.spardat.xma.mdl.util.TransStringSet#getAll()
     */
    public String[] getAll() {
        String [] toReturn = new String[size()];
        Iterator iter = selected_.iterator();
        for (int i=0; iter.hasNext(); i++) {
            toReturn[i] = (String) iter.next();
        }
        return toReturn;
    }
    
    /**
     * @see at.spardat.xma.mdl.util.TransStringSet#contains(java.lang.String)
     */
    public boolean contains (String key) {
        if (key == null) return false;
        return selected_.contains(key);
    }
    
    /**
     * @see at.spardat.xma.mdl.Synchronization#externalize(at.spardat.xma.serializer.XmaOutput, boolean)
     */
    public void externalize (XmaOutput o, boolean forceFull) throws IOException {
        // write the number of Strings in this
        o.writeInt("size", selected_.size());
        // the selection strings
        Iterator iter = selected_.iterator();
        while (iter.hasNext()) {
            String sel = (String) iter.next();
            o.writeString("sel", sel);
        }
    }

    /**
     * @see at.spardat.xma.mdl.Synchronization#internalize(at.spardat.xma.serializer.XmaInput)
     */
    public void internalize (XmaInput i) throws IOException, ClassNotFoundException {
        // read number of entries and reset HashSet
        selected_.clear();
        int numSelections = i.readInt();
        for (int j=0; j




© 2015 - 2024 Weber Informatics LLC | Privacy Policy