javolution.util.ConstantTable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javolution-core-java-msftbx Show documentation
Show all versions of javolution-core-java-msftbx Show documentation
Only the Java Core part of Javolution library, with slight modifications for use in MSFTBX.
/*
* Javolution - Java(TM) Solution for Real-Time and Embedded Systems
* Copyright (C) 2014 - Javolution (http://javolution.org/)
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software is
* freely granted, provided that this notice is preserved.
*/
package javolution.util;
import java.util.Collection;
import javolution.lang.Constant;
import javolution.util.internal.table.ConstantTableImpl;
import javolution.util.internal.table.ReversedTableImpl;
import javolution.util.internal.table.SubTableImpl;
import javolution.util.service.TableService;
import javolution.xml.DefaultXMLFormat;
import javolution.xml.XMLFormat;
import javolution.xml.stream.XMLStreamException;
/**
* A table for which immutability is guaranteed by construction.
*
* @author Jean-Marie Dautelle
* @version 6.1, February 2, 2014
*/
@Constant(comment = "Immutable")
@DefaultXMLFormat(ConstantTable.XML.class)
public class ConstantTable extends FastTable {
/**
* The default XML representation for constant tables
* (list of elements).
*/
public static class XML extends XMLFormat> {
@Override
public ConstantTable> newInstance(
Class extends ConstantTable>> cls, InputElement xml)
throws XMLStreamException {
int size = xml.getAttribute("size", 0);
Object[] elements = new Object[size];
for (int i = 0; i < size; i++) {
elements[i] = xml.getNext();
}
return ConstantTable.of(elements);
}
@Override
public void read(javolution.xml.XMLFormat.InputElement xml,
ConstantTable> that) throws XMLStreamException {
// Do nothing (read during instantiation).
}
@Override
public void write(ConstantTable> that,
javolution.xml.XMLFormat.OutputElement xml)
throws XMLStreamException {
int n = that.size();
xml.setAttribute("size", n);
for (int i = 0; i < n; i++) {
xml.add(that.get(i));
}
}
};
private static final long serialVersionUID = 0x600L; // Version.
/**
* Returns a new constant table holding the specified {@link Constant
* constant} elements.
*/
public static ConstantTable of(@Constant E... elements) {
return new ConstantTable(new ConstantTableImpl(elements));
}
/**
* Returns a new constant table holding the same elements as the specified
* collection (convenience method).
*/
@SuppressWarnings("unchecked")
public static ConstantTable of(Collection extends E> that) {
return ConstantTable.of((E[]) that.toArray(new Object[that.size()]));
}
/**
* Creates a constant table backed up by the specified{@link Constant
* constant} service implementation.
*/
protected ConstantTable(@Constant TableService service) {
super(service);
}
////////////////////////////////////////////////////////////////////////////
// Views.
//
@Constant
@Override
public ConstantTable atomic() {
return this; // Thread-Safe (unmodifiable)
}
@Constant
@Override
public ConstantTable reversed() {
return new ConstantTable(new ReversedTableImpl(service()));
}
@Constant
@Override
public ConstantTable shared() {
return this; // Thread-Safe (unmodifiable)
}
@Constant
@Override
public ConstantTable subTable(int fromIndex, int toIndex) {
return new ConstantTable(new SubTableImpl(service(), fromIndex,
toIndex));
}
@Constant
@Override
public ConstantTable unmodifiable() {
return this;
}
@Constant
@Override
protected TableService service() {
return super.service();
}
}