简体   繁体   English

如何在这种情况下声明Java Comparable

[英]How to declare Java Comparable in this situation

Here is a class I am working on: 这是我正在研究的课程:

public class Thing<T extends Comparable<? super T>> {
   private Map<String, List<SourcedValue<T>>> properties;
}

Then, SourcedValue is this: 然后, SourcedValue是这样的:

public class SourcedValue<T extends Comparable<? super T>> 
                         implements Comparable<****?*****> {
  private T value;
  private List<Sources> sources;

  @Override
  public int compareTo(SourcedValue<****?****> other) {
    return value.compareTo(other);
  }
}

What do I put into the ***?*** ? 我怎么把***?***

What I need to do is sort the List<SourcedValue<T>> in the Thing in some convert method that creates Thing and populates its properties , (along with its List<SourcedValue<T>> for each property). 我需要做的是在一些转换方法中对Thing中的List<SourcedValue<T>>进行排序,该方法创建Thing并填充其properties (以及每个属性的List<SourcedValue<T>> )。

I am not sure if this will help you, but this is one way I see to implement your requirement. 我不确定这是否会对您有所帮助,但这是我实现您的要求的一种方式。 I will be glad if this helps you. 如果这有助于你,我会很高兴的。

public class SourcedValue<T extends Comparable<? super T>>
    implements Comparable<SourcedValue<? super T>> {
private T value;
private List<Integer> sources;

@Override
public int compareTo(SourcedValue<? super T> o) {
    return value.compareTo((T)o.value);
    }
}

Also use of super appears to be redundant here. 此处使用super似乎也是多余的。 Even the below solution should produce the same result 即使是以下解决方案也应该产生相同的结果

public class SourcedValue<T extends Comparable<T>>
    implements Comparable<SourcedValue<T>> {
private T value;
private List<Integer> sources;

@Override
public int compareTo(SourcedValue<T> o) {
    return value.compareTo(o.value);
}
}

Here's how I might do it: 这是我可能会这样做的:

TypedProperty.java TypedProperty.java

/**
 * A TypedProperty class - more than String.
 * @link https://stackoverflow.com/questions/56498727/how-to-declare-java-comparable-in-this-situation
 */
public class TypedProperty<T extends Comparable<T>> implements Comparable<TypedProperty<T>> {

    private final T value;
    private final Class<T> type;

    public TypedProperty(T value, Class<T> clazz) {
        this.value = value;
        this.type = clazz;
    }

    public T getValue() {
        return value;
    }

    public Class<T> getType() {
        return type;
    }

    @Override
    public int compareTo(TypedProperty<T> other) {
        return this.value.compareTo(other.value);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TypedProperty<?> that = (TypedProperty<?>) o;

        if (!getValue().equals(that.getValue())) return false;
        return getType().equals(that.getType());
    }

    @Override
    public int hashCode() {
        int result = getValue().hashCode();
        result = 31 * result + getType().hashCode();
        return result;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("TypedProperty{");
        sb.append("value=").append(value);
        sb.append(", type=").append(type);
        sb.append('}');
        return sb.toString();
    }
}

A JUnit test to prove that it works: 一个JUnit测试来证明它的工作原理:

import org.junit.Assert;
import org.junit.Test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @link https://stackoverflow.com/questions/56498727/how-to-declare-java-comparable-in-this-situation
 */
public class TypedPropertyTest {

    @Test
    public void testConstructor_DateTypedProperty() throws ParseException {
        // setup
        String [] testDates = { "2019-06-07", "2018-03-17", "2017-01-01" };
        List<String> expected = Arrays.asList(testDates);
        Collections.sort(expected);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<TypedProperty<Date>> typedProperties = new ArrayList<>();
        for (String testDate : testDates) {
            typedProperties.add(new TypedProperty<>(format.parse(testDate), Date.class));
        }
        // test
        Collections.sort(typedProperties);
        // assert
        for (int i = 0; i < typedProperties.size(); ++i) {
            Assert.assertEquals(format.parse(expected.get(i)), typedProperties.get(i).getValue());
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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