com.cohort.array
Class Attributes

java.lang.Object
  extended by com.cohort.array.Attributes

public class Attributes
extends java.lang.Object

This class holds a list of attributes (name=value, where name is a String and value is a PrimitiveArray). The backing datastructure (ConcurrentHashMap) is thread-safe.


Field Summary
static java.lang.String ERROR
          "ERROR" is defined here (from String2.ERROR) so that it is consistent in log files.
static boolean verbose
          Set this to true (by calling verbose=true in your program, not but changing the code here) if you want lots of diagnostic messages sent to String2.log.
 
Constructor Summary
Attributes()
          This constructs a new, empty Attributes object.
Attributes(Attributes attributes)
          This constructs a new Attributes object which is a clone of 'attributes'.
Attributes(Attributes moreImportant, Attributes lessImportant)
          This constructs a new Attributes object which has the contents of moreImportant and lessImportant.
 
Method Summary
 Attributes add(Attributes moreImportant)
          This is like the similar set() method, but returns 'this'.
 Attributes add(java.lang.String name, byte value)
          This is like the similar set() method, but returns 'this'.
 Attributes add(java.lang.String name, char value)
          This is like the similar set() method, but returns 'this'.
 Attributes add(java.lang.String name, double value)
          This is like the similar set() method, but returns 'this'.
 Attributes add(java.lang.String name, float value)
          This is like the similar set() method, but returns 'this'.
 Attributes add(java.lang.String name, int value)
          This is like the similar set() method, but returns 'this'.
 Attributes add(java.lang.String name, long value)
          This is like the similar set() method, but returns 'this'.
 Attributes add(java.lang.String name, PrimitiveArray value)
          This is like the similar set() method, but returns 'this'.
 Attributes add(java.lang.String name, short value)
          This is like the similar set() method, but returns 'this'.
 Attributes add(java.lang.String name, java.lang.String value)
          This is like the similar set() method, but returns 'this'.
 void clear()
          This clears all the attributes being held.
 java.lang.Object clone()
          This makes a deep clone of the current table (data and attributes).
 void copyTo(Attributes destination)
          This makes destination's contents equal this Attribute's contents.
 boolean equals(java.lang.Object o)
          This tests if o is an Attributes and has the same data.
 PrimitiveArray get(java.lang.String name)
          This returns the value of a specific attribute (or null if the name isn't defined).
 double getDouble(java.lang.String name)
          A convenience method which returns the first element of the attribute's value PrimitiveArray as a double, regardless of the type used to store it.
 float getFloat(java.lang.String name)
          A convenience method which returns the first element of the attribute's value PrimitiveArray as a float, regardless of the type used to store it.
 int getInt(java.lang.String name)
          A convenience method which returns the first element of the attribute's value PrimitiveArray as an int, regardless of the type used to store it.
 long getLong(java.lang.String name)
          A convenience method which returns the first element of the attribute's value PrimitiveArray as a long, regardless of the type used to store it.
 java.lang.String[] getNames()
          This returns an array with the names of all of the attributes.
 double getNiceDouble(java.lang.String name)
          A convenience method which returns the first element of the attribute's value PrimitiveArray as a double, regardless of the type used to store it.
 java.lang.String getString(java.lang.String name)
          A convenience method which returns the first element of the attribute's value PrimitiveArray as a String, regardless of the type used to store it.
 java.lang.String[] getStringsFromCSV(java.lang.String name)
          A convenience method which assumes the first element of the attribute's value PrimitiveArray is a CSV String and which splits the string into parts.
 PrimitiveArray remove(java.lang.String name)
          This removes a specific attribute.
 void removeIfSame(Attributes otherAtts)
          This removes keys and values from this Attributes which are the same in otherAtts.
 void removeValue(java.lang.String value)
          This removes any entry which has a String value of 'value'.
 void set(Attributes moreImportant)
          This calls set() for all the attributes in 'additional'.
 PrimitiveArray set(java.lang.String name, byte value)
          A convenience method which stores the byte in an ByteArray then stores the attribute.
 PrimitiveArray set(java.lang.String name, char value)
          A convenience method which stores the char in an CharArray then stores the attribute.
 PrimitiveArray set(java.lang.String name, double value)
          A convenience method which stores the double in a DoubleArray then stores the attribute.
 PrimitiveArray set(java.lang.String name, float value)
          A convenience method which stores the float in a FloatArray then stores the attribute.
 PrimitiveArray set(java.lang.String name, int value)
          A convenience method which stores the int in an IntArray then stores the attribute.
 PrimitiveArray set(java.lang.String name, long value)
          A convenience method which stores the long in an LongArray then stores the attribute.
 PrimitiveArray set(java.lang.String name, PrimitiveArray value)
          This is the main method to set the value of a specific attribute (adding it if it doesn't exist, revising it if it does, or removing it if value is (PrimitiveArray)null).
 PrimitiveArray set(java.lang.String name, short value)
          A convenience method which stores the short in an ShortArray then stores the attribute.
 PrimitiveArray set(java.lang.String name, java.lang.String value)
          A convenience method which stores the String in a StringArray then stores the attribute.
 PrimitiveArray setIfNotAlreadySet(java.lang.String name, java.lang.String value)
          Like set, but only sets the value if there is no current value.
 int size()
          This returns the number of nameValue pairs in the data structure.
static void test()
          This tests the methods in this class.
 java.lang.String toNcString(java.lang.String prefix, java.lang.String suffix)
          This generates a String with "[prefix][name]=[value][suffix]" on each line.
 java.lang.String toString()
          This prints the attributes to a newline separated String, one per line: "    [name]=[value]".
static java.lang.String valueToNcString(PrimitiveArray pa)
          This returns a netcdf-style String representation of a PrimitiveArray: StringArray is newline separated, others are comma separated.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

verbose

public static boolean verbose
Set this to true (by calling verbose=true in your program, not but changing the code here) if you want lots of diagnostic messages sent to String2.log.


ERROR

public static final java.lang.String ERROR
"ERROR" is defined here (from String2.ERROR) so that it is consistent in log files.

See Also:
Constant Field Values
Constructor Detail

Attributes

public Attributes()
This constructs a new, empty Attributes object.


Attributes

public Attributes(Attributes attributes)
This constructs a new Attributes object which is a clone of 'attributes'.

Parameters:
attributes -

Attributes

public Attributes(Attributes moreImportant,
                  Attributes lessImportant)
This constructs a new Attributes object which has the contents of moreImportant and lessImportant.

Parameters:
moreImportant -
lessImportant -
Method Detail

clear

public void clear()
This clears all the attributes being held. The result isValid().


size

public int size()
This returns the number of nameValue pairs in the data structure.

Returns:
the number of nameValue pairs in the data structure.

clone

public java.lang.Object clone()
This makes a deep clone of the current table (data and attributes).

Overrides:
clone in class java.lang.Object
Returns:
a new Table.

get

public PrimitiveArray get(java.lang.String name)
This returns the value of a specific attribute (or null if the name isn't defined).

Parameters:
name -
Returns:
the attribute's value (a PrimitiveArray).

getNames

public java.lang.String[] getNames()
This returns an array with the names of all of the attributes.

Returns:
an array with the names of all of the attributes, sorted in a case-insensitive way.

getString

public java.lang.String getString(java.lang.String name)
A convenience method which returns the first element of the attribute's value PrimitiveArray as a String, regardless of the type used to store it.

Parameters:
name -
Returns:
the String attribute or null if trouble (e.g., not found)

getStringsFromCSV

public java.lang.String[] getStringsFromCSV(java.lang.String name)
A convenience method which assumes the first element of the attribute's value PrimitiveArray is a CSV String and which splits the string into parts.

Parameters:
name -
Returns:
a String[] or null if trouble (e.g., not found)

getDouble

public double getDouble(java.lang.String name)
A convenience method which returns the first element of the attribute's value PrimitiveArray as a double, regardless of the type used to store it.

Parameters:
name -
Returns:
the attribute as a double or Double.NaN if trouble (e.g., not found)

getNiceDouble

public double getNiceDouble(java.lang.String name)
A convenience method which returns the first element of the attribute's value PrimitiveArray as a double, regardless of the type used to store it. If the type was float, this returns Math2.floatToDouble(value).

Parameters:
name -
Returns:
the attribute as a nice double or Double.NaN if trouble (e.g., not found)

getFloat

public float getFloat(java.lang.String name)
A convenience method which returns the first element of the attribute's value PrimitiveArray as a float, regardless of the type used to store it.

Parameters:
name -
Returns:
the attribute as a float or Float.NaN if trouble (e.g., not found)

getLong

public long getLong(java.lang.String name)
A convenience method which returns the first element of the attribute's value PrimitiveArray as a long, regardless of the type used to store it.

Parameters:
name -
Returns:
the attribute as a long or Long.MAX_VALUE if trouble (e.g., not found)

getInt

public int getInt(java.lang.String name)
A convenience method which returns the first element of the attribute's value PrimitiveArray as an int, regardless of the type used to store it.

Parameters:
name -
Returns:
the attribute as an int or Integer.MAX_VALUE if trouble (e.g., not found)

remove

public PrimitiveArray remove(java.lang.String name)
This removes a specific attribute.

Parameters:
name - the name of the attribute
Returns:
the previous value stored for attributeName, or null if none

set

public PrimitiveArray set(java.lang.String name,
                          PrimitiveArray value)
This is the main method to set the value of a specific attribute (adding it if it doesn't exist, revising it if it does, or removing it if value is (PrimitiveArray)null).

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'. If value is null or size==0 or it is one String="", name is removed from attributes.
Returns:
the previous value stored for attributeName, or null if none

add

public Attributes add(java.lang.String name,
                      PrimitiveArray value)
This is like the similar set() method, but returns 'this'. add() lets you string several set commands together, e.g., (new Attributes()).add("name", "Bob").add("height", 197);

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'
Returns:
'this'

set

public void set(Attributes moreImportant)
This calls set() for all the attributes in 'additional'. In case of duplicate names, the 'additional' attributes have precedence.

Parameters:
moreImportant - the Attributes to be added.

add

public Attributes add(Attributes moreImportant)
This is like the similar set() method, but returns 'this'. add() lets you string several set commands together, e.g., (new Attributes()).add("name", "Bob").add("height", 197);

Parameters:
moreImportant - the Attributes to be added
Returns:
'this'

set

public PrimitiveArray set(java.lang.String name,
                          java.lang.String value)
A convenience method which stores the String in a StringArray then stores the attribute.

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'. If value is null or "", name is removed from attributes.
Returns:
the previous value stored for attributeName, or null if none

setIfNotAlreadySet

public PrimitiveArray setIfNotAlreadySet(java.lang.String name,
                                         java.lang.String value)
Like set, but only sets the value if there is no current value.

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'. If value is null or "", name is removed from attributes.
Returns:
the previous value stored for attributeName, or null if none

add

public Attributes add(java.lang.String name,
                      java.lang.String value)
This is like the similar set() method, but returns 'this'. add() lets you string several set commands together, e.g., (new Attributes()).add("name", "Bob").add("height", 197);

Parameters:
name - the name of the attribute
value - a String which is the value associate with the 'name'
Returns:
'this'

set

public PrimitiveArray set(java.lang.String name,
                          double value)
A convenience method which stores the double in a DoubleArray then stores the attribute.

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'
Returns:
the previous value stored for attributeName, or null if none

add

public Attributes add(java.lang.String name,
                      double value)
This is like the similar set() method, but returns 'this'. add() lets you string several set commands together, e.g., (new Attributes()).add("name", "Bob").add("height", 197);

Parameters:
name - the name of the attribute
value - a double which is the value associate with the 'name'
Returns:
'this'

set

public PrimitiveArray set(java.lang.String name,
                          float value)
A convenience method which stores the float in a FloatArray then stores the attribute.

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'
Returns:
the previous value stored for attributeName, or null if none

add

public Attributes add(java.lang.String name,
                      float value)
This is like the similar set() method, but returns 'this'. add() lets you string several set commands together, e.g., (new Attributes()).add("name", "Bob").add("height", 197);

Parameters:
name - the name of the attribute
value - a float which is the value associate with the 'name'
Returns:
'this'

set

public PrimitiveArray set(java.lang.String name,
                          long value)
A convenience method which stores the long in an LongArray then stores the attribute.

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'
Returns:
the previous value stored for attributeName, or null if none

add

public Attributes add(java.lang.String name,
                      long value)
This is like the similar set() method, but returns 'this'. add() lets you string several set commands together, e.g., (new Attributes()).add("name", "Bob").add("height", 197);

Parameters:
name - the name of the attribute
value - a long which is the value associate with the 'name'
Returns:
'this'

set

public PrimitiveArray set(java.lang.String name,
                          int value)
A convenience method which stores the int in an IntArray then stores the attribute.

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'
Returns:
the previous value stored for attributeName, or null if none

add

public Attributes add(java.lang.String name,
                      int value)
This is like the similar set() method, but returns 'this'. add() lets you string several set commands together, e.g., (new Attributes()).add("name", "Bob").add("height", 197);

Parameters:
name - the name of the attribute
value - an int which is the value associate with the 'name'
Returns:
'this'

set

public PrimitiveArray set(java.lang.String name,
                          short value)
A convenience method which stores the short in an ShortArray then stores the attribute.

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'
Returns:
the previous value stored for attributeName, or null if none

add

public Attributes add(java.lang.String name,
                      short value)
This is like the similar set() method, but returns 'this'. add() lets you string several set commands together, e.g., (new Attributes()).add("name", "Bob").add("height", 197);

Parameters:
name - the name of the attribute
value - a short which is the value associate with the 'name'
Returns:
'this'

set

public PrimitiveArray set(java.lang.String name,
                          char value)
A convenience method which stores the char in an CharArray then stores the attribute.

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'
Returns:
the previous value stored for attributeName, or null if none

add

public Attributes add(java.lang.String name,
                      char value)
This is like the similar set() method, but returns 'this'. add() lets you string several set commands together, e.g., (new Attributes()).add("name", "Bob").add("height", 197);

Parameters:
name - the name of the attribute
value - a char which is the value associate with the 'name'
Returns:
'this'

set

public PrimitiveArray set(java.lang.String name,
                          byte value)
A convenience method which stores the byte in an ByteArray then stores the attribute.

Parameters:
name - the name of the attribute
value - a PrimitiveArray which is the value associate with the 'name'
Returns:
the previous value stored for attributeName, or null if none

add

public Attributes add(java.lang.String name,
                      byte value)
This is like the similar set() method, but returns 'this'. add() lets you string several set commands together, e.g., (new Attributes()).add("name", "Bob").add("height", 197);

Parameters:
name - the name of the attribute
value - a byte which is the value associate with the 'name'
Returns:
'this'

toString

public java.lang.String toString()
This prints the attributes to a newline separated String, one per line: "    [name]=[value]".

Overrides:
toString in class java.lang.Object

removeValue

public void removeValue(java.lang.String value)
This removes any entry which has a String value of 'value'.


toNcString

public java.lang.String toNcString(java.lang.String prefix,
                                   java.lang.String suffix)
This generates a String with "[prefix][name]=[value][suffix]" on each line.

This nc-style version is used to print netcdf header attributes. It uses String2.toJson for String attributes.

Parameters:
prefix -
suffix -
Returns:
the desired string representation

copyTo

public void copyTo(Attributes destination)
This makes destination's contents equal this Attribute's contents. The values (primitiveArrays) are cloned.

Parameters:
destination - the Attributes which will be made equal to 'source'.

equals

public boolean equals(java.lang.Object o)
This tests if o is an Attributes and has the same data.

Overrides:
equals in class java.lang.Object
Parameters:
o - an object, presumably an Attributes
Throws:
java.lang.Exception - if a difference is found.

valueToNcString

public static java.lang.String valueToNcString(PrimitiveArray pa)
This returns a netcdf-style String representation of a PrimitiveArray: StringArray is newline separated, others are comma separated.

Parameters:
pa -
Returns:
a String representation of a value PrimitiveArray.

removeIfSame

public void removeIfSame(Attributes otherAtts)
This removes keys and values from this Attributes which are the same in otherAtts.

Parameters:
otherAtts -

test

public static void test()
                 throws java.lang.Exception
This tests the methods in this class.

Throws:
java.lang.Exception