下面列出了怎么用java.lang.Comparable的API类实例代码及写法,或者点击链接到github查看源代码。
@Test public void superclassReferencesSelf() throws Exception {
String source = JavaFile.builder("com.squareup.tacos",
TypeSpec.classBuilder("Taco")
.superclass(ParameterizedTypeName.get(
ClassName.get(Comparable.class), ClassName.get("com.squareup.tacos", "Taco")))
.build())
.build()
.toString();
assertThat(source).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "import java.lang.Comparable;\n"
+ "\n"
+ "class Taco extends Comparable<Taco> {\n"
+ "}\n");
}
@Test public void typeVariableWithBounds() {
AnnotationSpec a = AnnotationSpec.builder(ClassName.get("com.squareup.tacos", "A")).build();
TypeVariableName p = TypeVariableName.get("P", Number.class);
TypeVariableName q = (TypeVariableName) TypeVariableName.get("Q", Number.class).annotated(a);
TypeSpec typeSpec = TypeSpec.classBuilder("Location")
.addTypeVariable(p.withBounds(Comparable.class))
.addTypeVariable(q.withBounds(Comparable.class))
.addField(p, "x")
.addField(q, "y")
.build();
assertThat(toString(typeSpec)).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "import java.lang.Comparable;\n"
+ "import java.lang.Number;\n"
+ "\n"
+ "class Location<P extends Number & Comparable, @A Q extends Number & Comparable> {\n"
+ " P x;\n"
+ "\n"
+ " @A Q y;\n"
+ "}\n");
}
@Test public void classImplementsExtends() throws Exception {
ClassName taco = ClassName.get(tacosPackage, "Taco");
ClassName food = ClassName.get("com.squareup.tacos", "Food");
TypeSpec typeSpec = TypeSpec.classBuilder("Taco")
.addModifiers(Modifier.ABSTRACT)
.superclass(ParameterizedTypeName.get(ClassName.get(AbstractSet.class), food))
.addSuperinterface(Serializable.class)
.addSuperinterface(ParameterizedTypeName.get(ClassName.get(Comparable.class), taco))
.build();
assertThat(toString(typeSpec)).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "import java.io.Serializable;\n"
+ "import java.lang.Comparable;\n"
+ "import java.util.AbstractSet;\n"
+ "\n"
+ "abstract class Taco extends AbstractSet<Food> "
+ "implements Serializable, Comparable<Taco> {\n"
+ "}\n");
}
@Test public void interfaceExtends() throws Exception {
ClassName taco = ClassName.get(tacosPackage, "Taco");
TypeSpec typeSpec = TypeSpec.interfaceBuilder("Taco")
.addSuperinterface(Serializable.class)
.addSuperinterface(ParameterizedTypeName.get(ClassName.get(Comparable.class), taco))
.build();
assertThat(toString(typeSpec)).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "import java.io.Serializable;\n"
+ "import java.lang.Comparable;\n"
+ "\n"
+ "interface Taco extends Serializable, Comparable<Taco> {\n"
+ "}\n");
}
@Test public void typeVariables() throws Exception {
TypeVariableName t = TypeVariableName.get("T");
TypeVariableName p = TypeVariableName.get("P", Number.class);
ClassName location = ClassName.get(tacosPackage, "Location");
TypeSpec typeSpec = TypeSpec.classBuilder("Location")
.addTypeVariable(t)
.addTypeVariable(p)
.addSuperinterface(ParameterizedTypeName.get(ClassName.get(Comparable.class), p))
.addField(t, "label")
.addField(p, "x")
.addField(p, "y")
.addMethod(MethodSpec.methodBuilder("compareTo")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(int.class)
.addParameter(p, "p")
.addCode("return 0;\n")
.build())
.addMethod(MethodSpec.methodBuilder("of")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addTypeVariable(t)
.addTypeVariable(p)
.returns(ParameterizedTypeName.get(location, t, p))
.addParameter(t, "label")
.addParameter(p, "x")
.addParameter(p, "y")
.addCode("throw new $T($S);\n", UnsupportedOperationException.class, "TODO")
.build())
.build();
assertThat(toString(typeSpec)).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "import java.lang.Comparable;\n"
+ "import java.lang.Number;\n"
+ "import java.lang.Override;\n"
+ "import java.lang.UnsupportedOperationException;\n"
+ "\n"
+ "class Location<T, P extends Number> implements Comparable<P> {\n"
+ " T label;\n"
+ "\n"
+ " P x;\n"
+ "\n"
+ " P y;\n"
+ "\n"
+ " @Override\n"
+ " public int compareTo(P p) {\n"
+ " return 0;\n"
+ " }\n"
+ "\n"
+ " public static <T, P extends Number> Location<T, P> of(T label, P x, P y) {\n"
+ " throw new UnsupportedOperationException(\"TODO\");\n"
+ " }\n"
+ "}\n");
}
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* {@code null} otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* {@code null} otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;
/**
* Returns the maximal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the maximum value.
*/
public Comparable<?> getMaxValue() ;
/**
* Returns the minimal value for this parameter, if it has one, or
* <tt>null</tt> otherwise.
*
* @return the minimum value.
*/
public Comparable<?> getMinValue() ;