Why use a "static" class in this way? - java

I have a FutureContent class which just holds a static reference to a Future class. I'm not sure why it was done this way, it means every method call now does not have to be static. Is there another reason it was done this way ?
//One possible use of Future class
FutureContent.future.deleteContent("test");
public class FutureContent {
public static Future future = new Future();
}
public class Future{
private final Object lock = new Object();
private String str;
private Hashtable content = new Hashtable();
public void addContent(Object key, Object value){
synchronized(lock){
if(content.containsKey(key)){
content.remove(key);
content.put(key, value);
}
else {
content.put(key, value);
}
}
}
public void clearContent(){
content.clear();
}
public void deleteContent(Object key){
if(content.containsKey(key)){
content.remove(key);
}
}
public boolean getBoolean(Object key){
if(!content.contains(key)){
return false;
}
else {
return ((Boolean)content.get(key)).booleanValue();
}
}
public void addBoolean(Object key , boolean value){
Boolean b = new Boolean(value);
synchronized(lock){
if(content.containsKey(key)){
content.remove(key);
content.put(key, b);
}
else {
content.put(key, b);
}
}
}
public Object getContent(Object key){
return content.get(key);
}
public void setString(String str){
synchronized(lock){
this.str = str;
lock.notifyAll();
}
}
public String getString(){
synchronized(lock){
while(this.str == null)
try {
lock.wait();
} catch (InterruptedException e) {
return null;
}
return this.str;
}
}
private JSONObject value;
public void set(JSONObject t){
synchronized(lock){
value = t;
lock.notifyAll();
}
}
public JSONObject get(){
synchronized(lock){
while(value == null)
try {
lock.wait();
} catch (InterruptedException e) {
return null;
}
return value;
}
}
}

It's an approach to provide a pre-allocated object which is typically immutable and can be shared across instances. A common example is Collections.EMPTY_LIST with the signature
public static final List EMPTY_LIST = new EmptyList<Object>();

It is a way to use globals in Java. Although, we cannot directly create global variables in Java, we can achieve almost the same effect with public and static modifiers. Although, there are many disadvantages to globals, it has got some advantages also. We can easily exchange the data from one place, and that will be reflected everywhere. ie., You can store and access common data between different modules and classes, without any hurdles of passing data between them. But beware! -:)

Related

How do I compare the value of an Enum with all possible values of the Enum in an Enum method and avoid missing return statement?

I'm just learning about enumerations in Java. When I run the code below I get an error which I also reproduce below. Basically, my question is: when I define a method in an Enum, and in that method I want to check the value of the enum so that I can do something based on that value, how do I perform this check?
Below I have an Enum with three possible values, and in the method getNext, I have three if statements comparing the value of this Enum with each of the three possible values. But I still get an error saying that there is a path without a return.
package enumerations;
enum TrafficLightColor2 {
RED(12), GREEN(10), YELLOW(2);
private int waitTime;
TrafficLightColor2(int waitTime) {
this.waitTime = waitTime;
}
int getWaitTime() {
return waitTime;
}
TrafficLightColor2 getNext() {
if (this.equals(TrafficLightColor2.GREEN)) {
return TrafficLightColor2.YELLOW;
}
if (this.equals(TrafficLightColor2.YELLOW)) {
return TrafficLightColor2.RED;
}
if (this.equals(TrafficLightColor2.RED)) {
return TrafficLightColor2.GREEN;
}
}
}
// A computerized traffic light.
class TrafficLightSimulator2 implements Runnable {
private Thread thrd; // holds the thread that runs the simulation
private TrafficLightColor2 tlc; // holds the traffic light color
boolean stop = false; // set to true to stop the simulation
boolean changed = false; // true when the light has changed
TrafficLightSimulator2(TrafficLightColor2 init) {
tlc = init;
thrd = new Thread(this);
thrd.start();
}
TrafficLightSimulator2() {
tlc = TrafficLightColor2.RED;
thrd = new Thread(this);
thrd.start();
}
// Start up the light.
public void run() {
while (!stop) {
try {
Thread.sleep(tlc.getWaitTime());
} catch (InterruptedException exc) {
System.out.println(exc);
}
changeColor();
}
}
// Change color.
synchronized void changeColor() {
tlc = tlc.getNext();
changed = true;
notify(); // signal that the light has changed
}
// Wait until a light change occurs.
synchronized void waitForChange() {
try {
while (!changed)
wait(); // wait for light to change
changed = false;
} catch (InterruptedException exc) {
System.out.println(exc);
}
}
// Return current color.
synchronized TrafficLightColor2 getColor() {
return tlc;
}
// Stop the traffic light.
synchronized void cancel() {
stop = true;
}
}
class TrafficLightDemo2 {
public static void main(String args[]) {
TrafficLightSimulator tl =
new TrafficLightSimulator(TrafficLightColor.GREEN);
for (int i = 0; i < 9; i++) {
System.out.println(tl.getColor());
tl.waitForChange();
}
tl.cancel();
}
}
I get the error
$ javac enumerations/TrafficLightDemo2.java
enumerations/TrafficLightDemo2.java:26: error: missing return statement
}
^
1 error
TrafficLightColor2 getNext() {
if (this.equals(TrafficLightColor2.GREEN)) {
return TrafficLightColor2.YELLOW;
}
if (this.equals(TrafficLightColor2.YELLOW)) {
return TrafficLightColor2.RED;
}
if (this.equals(TrafficLightColor2.RED)) {
return TrafficLightColor2.GREEN;
}
}
This method doesn't return the value if all 3 if are false.
Add return at the and or better throw an error, e.g.
throw new IllegalArgumentException("Unsupported enum")
The advantage of using instance fields in enum classes is that you can associate implementation details easily with your constants that are independent from your API. In other words, you can easily associate data with your enum constants that would admit an elegant solution that you aren't forever married to in the case that, for example, you need to add a new enum constant.
So, you can greatly simplify your implementation while fulfilling the same contract as follows:
enum TrafficLightColor2 {
RED(2, 12),
GREEN(0, 10),
YELLOW(1, 2);
private int order; // implementation detail; non-exported
private int waitTime;
TrafficLightColor2(int ord, int waitTime) {
this.order = ord;
this.waitTime = waitTime;
}
int getWaitTime() {
return waitTime;
}
TrafficLightColor2 getNext() {
final int nextColor = (this.order + 1) % 3; // magic numbers introduce fragility
return Arrays.stream(TrafficLight2.values())
.filter(e -> e.order == nextColor)
.findAny()
.get();
}
}
This version has some advantages to your original implementation: it is easier to maintain since, if enum constants are added, the compiler will force you to add an order value. In the original, if you forgot to modify your if-else-block after adding a constant, your program would continue to work but it would not provide the correct behavior. And because the implementation of the order is hidden, you are free to remove it or change it to some other implementation at any time without affecting the correctness of your API.
Have you considered including the next state along with the declared values?
public enum TrafficLightColor2 {
RED(12, "GREEN"), GREEN(10, "YELLOW"), YELLOW(2, "RED");
int waitTime;
String nextState;
Configurations(int waitTime, String nextState) {
this.waitTime = waitTime;
this.nextState = nextState;
}
public int getWaitTime() {
return waitTime;
}
public String getNextState() {
return nextState;
}
}
With this you can get the next state as
TrafficLightColor2 trafficLightColor = TrafficLightColor2.GREEN;
System.out.println(TrafficLightColor2.valueOf(trafficLightColor.getNextState()));

Why a finally block can't modify a returned Integer type data from catch block. Java

So two facts I am aware:
1.Due to auto-boxing even Integer i = 9; i it is still considered an object type, not primitive type.
2.In a try-catch-finally block, the finally can modify the value being returned by a catch block only catch block returns an object, not primitive data.
So why following code still prints out:
10
When finally suppose to modify the returnVal to 20 because returnVal is NOT a primitive data type?
public class test1 {
Integer getInt() {
Integer returnVal = 10;
try {
//some code here that will cause an exception
}
catch(Exception e) {
return returnVal;
}
finally {
returnVal += 10;
}
return returnVal;
}
public static void main(String[] args) {
test1 tes = new test1();
System.out.println(tes.getInt());
}
}
The following:
returnVal += 10;
does not modify the object referenced by returnVal (that object is immutable). Instead, it creates a new object and changes returnVal to point to it. That has no effect on the original reference supplied to return - that original object is what's being returned.
Contrast this with
class Test2 {
public static class MutableInteger {
public Integer value;
public MutableInteger(Integer value) {
this.value = value;
}
}
MutableInteger getInt() {
MutableInteger returnVal = new MutableInteger(10);
try {
throw new Exception();
}
catch(Exception e) {
return returnVal;
}
finally {
returnVal.value += 10;
}
}
public static void main(String[] args) {
Test2 test = new Test2();
System.out.println(test.getInt().value);
}
}
Here, returnVal is mutable, and the finally block modifies the object instead of rebinding the reference. As a result, the code prints out 20.
The catch block completes the execution via return statement i.e. return returnVal, Since returnVal is of type Integer which is immutable object , modifying it in the finally block doesn't reflect the changes made to it. Had Integer been mutable object then you would see the value return as 20. E.g.
class MyInteger {
int value;
public MyInteger(int i) {
value = i;
}
public void add(int someVal) {
value += someVal;
}
#Override
public String toString() {
return "MyInteger{" +
"value=" + value +
'}';
}
}
public class test1 {
MyInteger getInt(){
MyInteger returnVal = new MyInteger(10);
try{
String[] students = {"Harry","Paul"};
System.out.println(students[3]);
}
catch(Exception e){
return returnVal;
}finally {
returnVal.add(10);
}
return returnVal;
}
public static void main(String[] args) {
test1 tes = new test1();
System.out.println(tes.getInt());
}
}
Output 20 because MyInteger is mutable object.
I did not see any problem. Finally changes an integer value just like expected
/**
*
* #author alikatkar
*/
public class FinallyTest {
public static int testInt() {
int result = 0;
try {
throw new Exception();
} catch (Exception e) {
result = 2;
System.out.println("result in catch:"+result);
return result = 2;
} finally {
return result = 3;
}
}
public static void main(String[] args) {
System.out.println("Returns:"+FinallyTest.testInt());
}
}
Output:
result in catch:2
Returns:3

Java Return between a int or String(unknown return type)

My program loads information from a text file and creates an array of an object with the information whether it is a integer or a string.
I then want the object to return either a String or an Integer depending on whether the object is holding a integer value or a string value.
edit...
So here is my type class that holds either a int if the field in the text file is a number, or a string if the field is a word, and this is held in a Type array.
public class Type {
private String name;
private int value;
public Type(String name) {
this.name = name;
}
public Type(int value) {
this.value = value;
}
public String getName() {
return this.name;
}
public int getValue() {
return this.value;
}
public boolean isInt() {
boolean isInt = false;
if (this.value != 0) {
isInt = true;
return isInt;
}
return isInt;
}
}
So in my array could be either a Int or a String, i want to return the datatype without any long statements in my main class.
If you strictly want only to get the specific values, you could add a method to your Type class and get the values from this method, ugly but does what you want:
public <T> T getDynamicValue(Type t) {
if (isInt()) {
return (T) ((Integer) t.getValue());
} else {
return (T) t.getName();
}
}
use of it:
List<Type> dynamicList = Arrays.asList(new Type[]{new Type(1), new Type(2), new Type("dog")});
for (Type t : dynamicList) {
System.out.println("T -> " + t.getDynamicValue(t));
}
If you want to perform some manipulation with this data, you have to make an instanceof check and Cast it, for instance some splitting (or String methods) with the name value...
You can't choose the type of object to return at runtime. Your only option is to return an Object. You can check if it's a String or an int using this code, for example:
if(object instanceof String) {
//... it's a string
}
else {
//...otherwise it's an int
}
If you are reading all inputs into String instances, you will need to test the values against Integer.parseString(value) to find out if it is actually an Integer.
You could try to cast the object into an Integer and catch the ClassCastException:
try {
int i = (Integer) object;
}
catch (ClassCastException e){
String s = (String) object;
}
When I have this type of problem, I sometimes solve it by turning the problem around and using a callback-style solution.
For example:
for ( Type t : array ) {
t.process( callback );
}
Where the callback looks like this:
interface Callback {
public void processInt(....);
public void processString(....);
}
You can then either implement the process method either with an if (isInt()) callback.processInt() else callback.processString(), or if you change the definition of Type you can use the inheritance tree to do it for you.
For example:
interface Type {
public void process( Callback cb );
}
class IntType implements Type {
public void process( Callback cb ) {
cb.processInt(...);
}
}
class StringType implements Type {
public void process( Callback cb ) {
cb.processString(...);
}
}

java generics T extends Simpletype?

I'd like to write a method, that does return something of a PrimitiveType like float, integer, boolean and also String if possible. I'd like to use generics for it but i stuck and dont find a solution for it. I do need it for a Configparser. Ill use it to get different values from the Config.
Current it des look like this and i know that the switch does not work like this but you get an idea of what id like to do:
public class ConfigurationManager extends XmlReader {
private final static String FILE_PATH = "config/config.cfg";
private static Element xml;
public ConfigurationManager() throws IOException {
FileHandle handle = Gdx.files.internal(FILE_PATH);
this.xml = this.parse(handle);
}
public Resolution getResolution() {
Resolution r = new Resolution();
r.height = xml.getFloat("height");
r.width = xml.getFloat("width");
return r;
}
public static <T> T getConfig(Class<T> type, String name) {
if (type.equals(Integer.class)) {
return type.cast(xml.getInt(name));
} else if (type.equals(Float.class)) {
return type.cast(xml.getFloat(name));
} else if (type.equals(Boolean.class)) {
return type.cast(xml.getBoolean(name));
} else if (type.equals(String.class)) {
return type.cast(xml.get(name));
}
throw new AssertionError("Invalid type");
}
}
Thanks alot
Well, I don't think you can do it with primitive types directly, but how about something like this:
public static <T> T getConfig(Class<T> type, String name) {
if(type.equals(Integer.class)){
return type.cast(xml.getInteger(name));
} else if(type.equals(Float.class)){
return type.cast(xml.getFloat(name));
} else if(type.equals(Double.class)) {
return type.cast(xml.getDouble(name));
} else if(type.equals(String.class)) {
return type.cast(xml.getString(name));
}
throw new AssertionError("Invalid type");
}
You could use an Enum to avoid the branching logic and the explicit casting.
public enum TypeSelector {
INTEGER() {
#Override
public Integer getValue(Elements xml, String name) {
return xml.getInteger(name);
}
},
DOUBLE() {
#Override
public Double getValue(Elements xml, String name) {
return xml.getDouble(name);
}
};
private static final Map<Class<?>, TypeSelector> SELECTORS = new HashMap<Class<?>, TypeSelector>() {
{
put(Integer.class, INTEGER);
put(Double.class, DOUBLE);
}
};
public static <T> TypeSelector getSelectorForType(Class<T> c) {
TypeSelector selector = SELECTORS.get(c);
if (selector == null) {
throw new AssertionError("Invalid type");
}
return selector;
}
public abstract <T> T getValue(Elements xml, String name);
}

Real time sorted by value, auto-discarding, bounded collection ?

I spent some time to try to make a collection that:
1) is sorted by value (not by key)
2) is sorted each time an element is added or modified
3) is fixed size and discard automatically smallest/biggest element depending of the sort way
4) is safe thread
So 3) and 4) I think it is quite ok. For 1) and 2) it was a bit more tricky. I spent quite a long time on this thread, experimenting the different sample, but one big issue is that the collection are sorted only once when object are inserted.
Anyway, I try to implement my own collection, which is working (shouldn't be used for huge data as it is sorted quite often) but I'm not so happy with the design. Especially in the fact that my value objects are constrained to be Observable (which is good) but not comparable so I had to use a dirty instanceof + exception for this.
Any sugestion to improve this ?
Here is the code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
public class SortedDiscardingSyncArray<K, V extends Observable> implements Observer {
// Comparison way (ascendent or descendant)
public static enum ComparisonWay
{
DESC,
ASC;
}
// this is backed by a List (and ArrayList impl)
private List<ArrayElement> array;
// Capacity, configurable, over this limit, an item will be discarded
private int MAX_CAPACITY = 200;
// default is descending comparison
private ComparisonWay compareWay = ComparisonWay.DESC;
public SortedDiscardingSyncArray(ComparisonWay compareWay, int mAX_CAPACITY) {
super();
this.compareWay = compareWay;
MAX_CAPACITY = mAX_CAPACITY;
array = new ArrayList <ArrayElement>(MAX_CAPACITY);
}
public SortedDiscardingSyncArray(int mAX_CAPACITY) {
super();
MAX_CAPACITY = mAX_CAPACITY;
array = new ArrayList<ArrayElement>(MAX_CAPACITY);
}
public SortedDiscardingSyncArray() {
super();
array = new ArrayList <ArrayElement>(MAX_CAPACITY);
}
public boolean put(K key, V value)
{
try {
return put (new ArrayElement(key, value, this));
} catch (Exception e) {
e.printStackTrace();
return false;
}
finally
{
sortArray();
}
}
private synchronized boolean put(ArrayElement ae)
{
if (array.size() < MAX_CAPACITY)
{
return array.add(ae);
}
// check if last one is greater/smaller than current value to insert
else if (ae.compareTo(array.get(MAX_CAPACITY-1)) < 0)
{
array.remove(MAX_CAPACITY - 1);
return array.add(ae);
}
// else we don't insert
return false;
}
public V getValue (int index)
{
return array.get(index).getValue();
}
public V getValue (K key)
{
for (ArrayElement ae : array)
{
if (ae.getKey().equals(key)) return ae.getValue();
}
return null;
}
public K getKey (int index)
{
return array.get(index).getKey();
}
private void sortArray()
{
Collections.sort(array);
}
public synchronized void setValue(K key, V newValue) {
for (ArrayElement ae : array)
{
if (ae.getKey().equals(key))
{
ae.setValue(newValue);
return;
}
}
}
public int size() {
return array.size();
}
#Override
public void update(java.util.Observable arg0, Object arg1) {
sortArray();
}
public static void main(String[] args) {
// some test on the class
SortedDiscardingSyncArray<String, ObservableSample> myData = new SortedDiscardingSyncArray<String, ObservableSample>(ComparisonWay.DESC, 20);
String Ka = "Ka";
String Kb = "Kb";
String Kc = "Kc";
String Kd = "Kd";
myData.put(Ka, new ObservableSample(0));
myData.put(Kb, new ObservableSample(3));
myData.put(Kc, new ObservableSample(1));
myData.put(Kd, new ObservableSample(2));
for (int i=0; i < myData.size(); i++)
{
System.out.println(myData.getKey(i).toString() + " - " + myData.getValue(i).toString());
}
System.out.println("Modifying data...");
myData.getValue(Kb).setValue(12);
myData.getValue(Ka).setValue(34);
myData.getValue(Kd).setValue(9);
myData.getValue(Kc).setValue(19);
for (int i=0; i < myData.size(); i++)
{
System.out.println(myData.getKey(i).toString() + " - " + myData.getValue(i).toString());
}
}
private class ArrayElement implements Comparable <ArrayElement> {
public ArrayElement(K key, V value, Observer obs) throws Exception {
super();
// don't know how to handle that case
// maybe multiple inheritance would have helped here ?
if (! (value instanceof Comparable)) throw new Exception("Object must be 'Comparable'");
this.key = key;
this.value = value;
value.addObserver(obs);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(key);
sb.append(" - ");
sb.append(value);
return sb.toString();
}
private K key;
private V value;
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public synchronized void setValue(V value) {
this.value = value;
}
#SuppressWarnings("unchecked")
#Override
public int compareTo(ArrayElement o) {
int c;
if (compareWay == ComparisonWay.DESC) c = ((Comparable<V>) o.getValue()).compareTo(this.getValue());
else c = ((Comparable<V>) this.getValue()).compareTo(o.getValue());
if (c != 0) {
return c;
}
Integer hashCode1 = o.getValue().hashCode();
Integer hashCode2 = this.getValue().hashCode();
// we don't check the compare way for hash code (useless ?)
return hashCode1.compareTo(hashCode2);
}
}
}
And the other class for testing purpose:
import java.util.Observable;
public class ObservableSample extends Observable implements Comparable <ObservableSample>
{
private Integer value = 0;
public ObservableSample(int value) {
this.value = value;
setChanged();
notifyObservers();
}
public String toString()
{
return String.valueOf(this.value);
}
public void setValue(Integer value) {
this.value = value;
setChanged();
notifyObservers();
}
public Integer getValue() {
return value;
}
#Override
public int compareTo(ObservableSample o) {
int c;
c = (this.getValue()).compareTo(o.getValue());
if (c != 0) {
return c;
}
Integer hashCode1 = o.getValue().hashCode();
Integer hashCode2 = this.getValue().hashCode();
// we don't check the compare way for hash code (useless ?)
return hashCode1.compareTo(hashCode2);
}
}
Collections are difficult to write, maybe you should look for an existing implementation.
Try checking out ImmutableSortedSet from Guava.
You can have a marker interface
public interface ComparableObservable extends Observable, Comparable {
}
and then change
SortedDiscardingSyncArray<K, V extends Observable>
to
SortedDiscardingSyncArray<K, V extends ComparableObservable>
to avoid the explicit cast.
Other than that the code is quite verbose and I didn't follow it completely. I would also suggest having a look at guava or (apache) commons-collections library to explore if you can find something reusable.
You can write generic wildcards with multiple bounds. So change your declaration of <K, V extends Observable> to <K, V extends Observable & Comparable<V>> and then you can treat V as if it implements both interfaces, without an otherwise empty and useless interface.
Another few things: Pick a naming convention, and stick with it. The one I use is that a name such as MAX_CAPACITY would be used for a static final field (i.e. a constant, such as a default) and that the equivalent instance field would be maxCapacity Names such as mAX_CAPACITY would be right out of the question.
See: Oracle's naming conventions for Java
Instead of using a ComparisonWay enum, I would take a custom Comparator. Much more flexible, and doesn't replicate something that already exists.
See: the Comparator API docs
Your code, as written, is not thread safe. In particular an observed element calling the unsynchronized update method may thus invoke sortArray without obtaining the proper lock. FindBugs is a great tool that catches a lot of problems like this.
Your ObservableSample does not really follow good practices with regards to how it implements Comparable, in that it does not really compare data values but instead the hashCode. The hashCode is essentially arbitrary and collisions are quite possible. Additionally, the Comparable interface requests that usually you should be "consistent with Equals", for which you also might want to take a look at the documentation for the Object class's equals method
Yes, it sounds like a lot of work, but if you go through it and do it right you will save yourself astounding amounts of debugging effort down the road. If you do not do these properly and to the spec, you will find that when you place it in Sets or Maps your keys or values strangely disappear, reappear, or get clobbered. And it will depend on which version of Java you run, potentially!
Here is a version updated. Still not completly sure it is safe thread but findbugs tool didn't give so usefull tips. Also for the comparisonWay, I don't want to constraint the user to develop its own comparator, I want to keep the things simple.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
public class SortedDiscardingSyncArray<K, V extends Observable & Comparable<V>> implements Observer {
// Comparison way (ascendent or descendant)
public static enum ComparisonWay { DESC, ASC; }
// this is backed by a List (and ArrayList)
private List<ArrayElement> array;
// Capacity, configurable, over this limit, an item will be discarded
private int maxCapacity = 200;
// default is descending comparison
private ComparisonWay compareWay = ComparisonWay.DESC;
public SortedDiscardingSyncArray(ComparisonWay compareWay, int maxCapacity) {
super();
this.compareWay = compareWay;
this.maxCapacity = maxCapacity;
array = new ArrayList <ArrayElement>(maxCapacity);
}
public SortedDiscardingSyncArray(int maxCapacity) {
super();
this.maxCapacity = maxCapacity;
array = new ArrayList<ArrayElement>(maxCapacity);
}
public SortedDiscardingSyncArray() {
super();
array = new ArrayList <ArrayElement>(maxCapacity);
}
// not synchronized, but calling internal sync put command
public boolean put(K key, V value)
{
try {
return put (new ArrayElement(key, value, this));
} catch (Exception e) {
e.printStackTrace();
return false;
}
finally
{
sortArray();
}
}
private synchronized boolean put(ArrayElement ae)
{
if (array.size() < maxCapacity) return array.add(ae);
// check if last one is greater/smaller than current value to insert
else if (ae.compareTo(array.get(maxCapacity-1)) < 0)
{
array.remove(maxCapacity - 1);
return array.add(ae);
}
// else we don't insert and return false
return false;
}
public V getValue (int index)
{
return array.get(index).getValue();
}
public V getValue (K key)
{
for (ArrayElement ae : array)
{
if (ae.getKey().equals(key)) return ae.getValue();
}
return null;
}
public K getKey (int index)
{
return array.get(index).getKey();
}
private synchronized void sortArray()
{
Collections.sort(array);
}
public synchronized void setValue(K key, V newValue) {
for (ArrayElement ae : array)
{
if (ae.getKey().equals(key))
{
ae.setValue(newValue);
return;
}
}
}
public int size() {
return array.size();
}
#Override
public void update(java.util.Observable arg0, Object arg1) {
sortArray();
}
public static void main(String[] args) {
// some test on the class
SortedDiscardingSyncArray<String, ObservableSample> myData = new SortedDiscardingSyncArray<String, ObservableSample>(ComparisonWay.DESC, 20);
String Ka = "Ka";
String Kb = "Kb";
String Kc = "Kc";
String Kd = "Kd";
myData.put(Ka, new ObservableSample(0));
myData.put(Kb, new ObservableSample(3));
myData.put(Kc, new ObservableSample(1));
myData.put(Kd, new ObservableSample(2));
for (int i=0; i < myData.size(); i++)
{
System.out.println(myData.getKey(i).toString() + " - " + myData.getValue(i).toString());
}
System.out.println("Modifying data...");
myData.getValue(Kb).setValue(12);
myData.getValue(Ka).setValue(34);
myData.getValue(Kd).setValue(9);
myData.getValue(Kc).setValue(19);
for (int i=0; i < myData.size(); i++)
{
System.out.println(myData.getKey(i).toString() + " - " + myData.getValue(i).toString());
}
}
private class ArrayElement implements Comparable <ArrayElement> {
public ArrayElement(K key, V value, Observer obs) throws Exception {
super();
this.key = key;
this.value = value;
value.addObserver(obs);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(key);
sb.append(" - ");
sb.append(value);
return sb.toString();
}
private K key;
private V value;
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public synchronized void setValue(V value) {
this.value = value;
}
#Override
public int compareTo(ArrayElement o) {
int c;
if (compareWay == ComparisonWay.DESC) c = o.getValue().compareTo(this.getValue());
else c = this.getValue().compareTo(o.getValue());
if (c != 0) {
return c;
}
Integer hashCode1 = o.getValue().hashCode();
Integer hashCode2 = this.getValue().hashCode();
// we don't check the compare way for hash code (useless ?)
return hashCode1.compareTo(hashCode2);
}
}
}

Categories

Resources