简体   繁体   中英

reverse method for generic pair in java

public class Main{

public static void main(String[] args) {

    final Pair<Integer> p = new DefaultPair<>(3,5);
    p.reverse();
    final Pair<Integer> q = new DefaultPair<>(5,3);
    }

}

public interface Pair<F> {
    F first();
    F second();
    F reverse();
}

public class DefaultPair<F> implements Pair<F> {
    private final F first; 
    private final F second; 
    private F reverseFirst; 
    private F reverseSecond; 


  public DefaultPair(F first, F second){//constructor 
  this.first = first; 
  this.second = second; 
    }

// method that is not working    
 @Override
  public F reverse() {

    this.reverseFirst = second; 
    this.reverseSecond = first; 

    System.out.println(this);
    return (F)this; 
   }

 @Override
 public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((first == null) ? 0 : first.hashCode());
    result = prime * result + ((second == null) ? 0 : second.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof DefaultPair))
        return false;
    DefaultPair<F> other = (DefaultPair) obj;
    if (first == null) {
        if (other.first != null)
            return false;
    } else if (!first.equals(other.first))
        return false;
    if (second == null) {
        if (other.second != null)
            return false;
    } else if (!second.equals(other.second))
        return false;
    return true;
 }

@Override
  public F first(){
      return first;
  }

 @Override
  public F second(){
      return second; 
  }


 @Override
 public String toString() {
      return "<" + first + ", " + second + ">";
    }

}

import static org.junit.Assert.*;

import org.junit.Test;

public class TestDefaultPair {

    @Test
    public void test() {
        final Pair<Integer> p = new DefaultPair<>(3, 5);
        final Pair<Integer> q = new DefaultPair<>(5, 3);
         assertEquals(3, p.first().intValue());
         assertEquals(5, p.second().intValue());
         assertEquals("<3, 5>", p.toString());
         assertTrue(p.equals(p));
         assertFalse(p.equals(q));
         assertFalse(q.equals(p));
         assertFalse(p.equals(null));
         assertTrue(p.equals(q.reverse()));//test that is not passing
    }

}

I need to have all of the test cases pass. All of them pass besides the last one where I have to invoke the reverse method. The receiver of the reverse method should not be mutated. For example, I should not put in p(3,5) to be reversed. Everything else works besides the reverse method. If i call p.reverse(); and print out p, it prints in the original order.

Your equals() implementation in DefaultPair checks first and second field values for equality. But your reverse() method only changes the values of reverseFirst and reverseSecond fields.

Either you need to use reverseFirst and reverseSecond in equals() or change the values of first and second in reverse()

我认为这是问题所在: F reverse() ,在您的情况下,F是Integer,但是Integer不是您应该返回的内容,您应该返回Pair。

p.equals(q.reverse()) is like: pair.equals(Integer)

Hopefully i did understand everything correctly, but i think your reverse method should be looking like this:

public DefaultPair<F> reverse() {
   return new DefaultPair<F>(second,first);
   // Here you should return a new instance of `DefaultPair<F>`, 
   // which uses second as first and first as second.
   // Now you are left with an non mutable reversed new instance of DefaultPair

}

In the end this would logically also make reverseFirst and reverseSecond useless in your class, and as though they should be removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM