skiwi
skiwi

Reputation: 69409

How to have a simpler constructor fo special cases

I am wondering if what I am trying to achieve is actually possible at all, I do not know the exact name of what I am doing hence why I cannot really properly google for results and why this topic title is also kind of vague.

My classes:

AccountConstraint.java:

package dao.constraint;

public class AccountConstraint {
    private Range<Integer> accountId;
    private String username;
    private String password;
    private String email;

    public AccountConstraint(final Range<Integer> accountId, final String username, final String password, final String email) {
        this.accountId = accountId;
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public Range<Integer> getAccountId() {
        return accountId;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }
}

Range.java:

package dao.constraint;

public class Range<T> {
    private T min;
    private T max;

    public Range(T min, T max) {
        this.min = min;
        this.max = max;
    }

    public T getMin() {
        return min;
    }

    public T getMax() {
        return max;
    }
}

A perfectly valid code example would be:

AccountConstraint ac = new AccountConstraint(new Range<Integer>(5, 10), null, null, null));

If you would want to get all accounts with id 5 to 10. A still valid, but weirder piece of code would be:

AccountConstraint ac = new AccountConstraint(new Range<Integer>(3, 3), null, null, null));

if you would want to get the account with id 3.

What I would like is:

AccountConstraint ac = new AccountConstraint(3, null, null, null);

as new Range<Integer>(3, 3) is theoretically equal to 3.

Is there a way to do this, possibly via adding code to the Range class, I kind of feel like this should be possible, but I got no clue how and/or where to start.

Upvotes: 1

Views: 143

Answers (6)

Bohemian
Bohemian

Reputation: 425428

You could change your Range class to add a single parameter constructor:

public Range(T t) {
    min = t;
    max = t;
}

And call that constructor in a separate AccountConstraint constructor.


Incidentally, your Range class could benefit from some type bounding:

public class Range<T extends Comparable<T>> {
    public boolean contains(T value) {
        return value.compareTo(min) >= 0 
          && value.compareTo(max) <= 0;
    }
    // rest of class the same 
}

Upvotes: 0

stinepike
stinepike

Reputation: 54742

Create a constructor like this. Pass the value to the constructor

   public AccountConstraint(Integer i, final String username, final String password, final String email) {
            this.accountId = new Range<Integer>(i,i)
            this.username = username;
            this.password = password;
            this.email = email;
        }

Upvotes: 0

Abubakkar
Abubakkar

Reputation: 15664

You can try this (Creating a new overloaded constructor):

public AccountConstraint(final Range<Integer> accountId, final String username, final String password, final String email) {
      this.accountId = accountId;
      this.username = username;
      this.password = password;
      this.email = email;
}

//new overloaded constructor
public AccountConstraint(Integer value, final String username, final String password, final String email) {
      Range<Integer> accountId = new Range(value,value);
      super(accountId , username, password, email));
}

Upvotes: 0

Rapha&#235;l
Rapha&#235;l

Reputation: 3751

Add a constructor taking an Integer as first argument and create a Range with it:

public AccountConstraint(Integer accountId, final String username, final String password, final String email) {
    this(new Range<Integer>(accountId, accountId), username, password, email);
}

Upvotes: 0

maqjav
maqjav

Reputation: 2434

Create more constructors:

public AccountConstraint(Integer accountId, final String username, final String password, final String email) {
    this.accountId = new Range<Integer>(accountId);
    this.username = username;
    this.password = password;
    this.email = email;
}

public Range(T num) {
    this.min = num;
    this.max = num;
}

Upvotes: 1

Mattias Buelens
Mattias Buelens

Reputation: 20179

You don't need to do anything about your Range class, simply provide another AccountConstraint constructor which makes a Range out of one int:

public AccountConstraint(final int accountId, final String username, final String password, final String email) {
    this(new Range<Integer>(accountId, accountId), username, password, email);
}

Upvotes: 5

Related Questions