开发者

Custom number class to be used as out parameter

There is no "out" or "ref" parameter in Java (I maybe wrong because I haven't touched Java in 3 yrs.). I am thinking create classes, like MyInteger, MyFloat and MyDouble, to be used as out parameter. is there a way to combine them into ONE generic class?

Sample code for MyInteger class:

public class MyInteger
{
   private int value = 0;

   public int getValue(){ return value;}
   public void setValue(int newValue) {value = newValue;}

}

EDIT:

how to use MyInteger class:

public boolean aMethod(int input, MyInteger output)
{
   boolean status = true;
   //calculation here;
   //set status to false if anything wrong;
   //if nothing wrong do this: out开发者_JS百科put.setValue(newValue);
   return status;
}

EDIT 2:

What I am asking is I hope I can combine MyInteger, MyFloat, ... into ONE generic class.


You can use AtomicInteger, AtomicLong, AtomicReference etc. They do exactly what you are suggesting, and as an added feature they are highly thread-safe


And in response to this comment:

but why there is no AtomicFloat and AtomicDouble

Here's what the java.util.concurrent.atomic package JavaDocs say:

Additionally, classes are provided only for those types that are commonly useful in intended applications. For example, there is no atomic class for representing byte. In those infrequent cases where you would like to do so, you can use an AtomicInteger to hold byte values, and cast appropriately. You can also hold floats using Float.floatToIntBits and Float.intBitstoFloat conversions, and doubles using Double.doubleToLongBits and Double.longBitsToDouble conversions.

My own take: That sounds awfully complicated, I'd go with AtomicReference<Double>, AtomicReference<Float> etc. instead


Besides the fact that out parameters are somewhat awkward and I would not recommend them, why don't you use Integer and the like?

Since they all extend Number you could do:

public class MyNumber<T extends Number>
{
 private T value = null;

 ...
}


IMHO, if you are using Objects / Visitor patterns properly, you should never need to return more than one value from a method.

Return an object which contains more than one value

public Pair<Integer, Double> method3();

or use a visitor to recieve more than one value. Useful if you can many out comes/errors.

public interface Visitor {
    public void onValues(int i, double d);
    public void onOtherValues(double d, String message);
}

method(Visitor visitor);

or you can update the object method is called on, rather than return the values.

public void method() {
    this.i = 10;
    this.d = 100.0;
}

e.g.

Worker worker = new Worker();
worker.method();
int i = worker.i;
double d = worker.d;

or you can return an valid value on a condition.

// returns the number of bytes read or -1 on the end of the file.
public int read(byte[] bytes);

// index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1)
public int binarySearch(Object[] array, Object key);

There is a generic holder you can use AtomicReference

public void method(AtomicReference<Integer> i, AtomicReference<Double> i);

For some types there is a built in type AtomicBoolean, AtomicInteger, AtomicLong

public void method(AtomicBoolean flag, AtomicInteger len);
// OR
public boolean method(AtomicInteger len);

You can also use a plain array

int[] i = { 0 };
double[] d = { 0.0 };
method2(i, d);

public void method2(int[] i, double[] d);


You can have your new classes subclass Number. The only reason Integer, Float, etc. are immutable is because that's how they're implemented.

Alternately, you could create a NumberHolder class that you pass around, that has a mutable Number field.

class NumberHolder {

  private Number num;

  public void setNumber(Number num) {
    this.num = num;
  }

  public void getNumber() {
    return num'
  }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜