commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From S├ębastien Brisard <sebastien.bris...@m4x.org>
Subject Re: [math] Read-only RealVector
Date Thu, 11 Aug 2011 08:08:42 GMT
2011/8/11 Arne Ploese <aploese@gmx.de>:
> What methods do you need?
>
> Maybe an interface with:
> public interface SimpleRealVector {
>  double getEntry(int i);
>  int getDimension();
> }
> will do?
>
No, I'd like to have *all* methods of the o.a.c.m.linear.RealVector
interface, *except* those which modify the caller object, e.g.
xxxToSelf and setXxx.
My question was actually more general. To make an object unmodifiable,
is it good practice to encapsulate it into another object, with
seamingly the same interface, but unimplemented methods which might
modify the object ? My point would be not to create any new
interface/abstract class.

Below is a quick example (which I hope is not buggy).
Thanks for your advice,
S├ębastien

/**********/
public abstract class AbstractFoo{
  public abstract double getValue();

  public abstract void setValue(final double x);

  public abstract AbstractFoo add(AbstractFoo bar);

  public void addToSelf(AbstractFoo bar){
    setValue(getValue + bar.getValue());
  }
}

/**********/
public class  Foo extends AbstractFoo{
  private double value;

  public Foo(final double x){
    value = x;
  }

  public double getValue(){
    return value;
  }

  public void setValue(final double x){
    value = x;
  }

  public AbstractFoo add(AbstractFoo bar){
    return new Foo(value + bar.getValue());
  }
}

/**********/
public final class FooReadOnly extends AbstractFoo{
  private final Foo foo;

  public FooReadOnly(AbstractFoo foo){
    this.foo = foo;

  public double getValue(){
    return foo.getValue();
  }

  public void setValue(final double x){
    throw new NotImplementedException("read only object");
  }

  public AbstractFoo add(AbstractFoo bar){
    return foo.add(bar);
  }

  public void addToSelf(AbstractFoo bar){
    throw new NotImplementedException("read only object");
  }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message