@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, 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 classImplementsExtendsSameName() throws Exception { ClassName javapoetTaco = ClassName.get(tacosPackage, "Taco"); ClassName tacoBellTaco = ClassName.get("com.taco.bell", "Taco"); ClassName fishTaco = ClassName.get("org.fish.taco", "Taco"); TypeSpec typeSpec = TypeSpec.classBuilder("Taco") .superclass(fishTaco) .addSuperinterface(ParameterizedTypeName.get(ClassName.get(Comparable.class), javapoetTaco)) .addSuperinterface(tacoBellTaco) .build(); assertThat(toString(typeSpec)).isEqualTo("" + "package com.squareup.tacos;\n" + "\n" + "import java.lang.Comparable;\n" + "\n" + "class Taco extends org.fish.taco.Taco " + "implements Comparable<Taco>, com.taco.bell.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"); }
/** * gibt das mittlere Element des (umgeordneten Arrays) zur�ck */ private static int partition(ArrayList<Comparable> list, int m, int n) { Comparable x = list.get(m); // Pivot-Element int j = n + 1; int i = m - 1; while (true) { j--; while (list.get(j).compareTo(x) > 0) j--; i++; while (list.get(i).compareTo(x) < 0) i++; if (i < j) exchange(list, i, j); else return j; } }
public void foo() { Runnable r = new Runnable() { @Override public void run() { Comparable<Integer> c = new Comparable<Integer>() { @Override public int compareTo(Integer o) { return 0; } }; } }; class FooLocal { Runnable r = new Runnable() { @Override public void run() { } }; } }
public static <T extends Comparable<T>> Node<T> mergeLists( Node<T> a, Node<T> b) { if (a == null && b == null) { return null; } if (a == null) { return b; } if (b == null) { return a; } Node<T> temp = a.next; a.next = b.next; a.next.prev = a; b.next = temp; b.next.prev = b; return a.compareTo(b) < 0 ? a : b; }
@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 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"); }
public void foo() { Comparable<String> c = (o) -> ba<caret>r(); }
public <T extends Comparable> T genericMethod(T genericArg) { return genericArg; }
public int compare(Object o1, Object o2) { if( (o1 == null) || (o2 == null) ) { throw new ClassCastException( "There were nulls in the arguments for this method: "+ "compare("+o1 + ", " + o2 + ")" ); } if(o1 instanceof Comparable) { if(o2 instanceof Comparable) { int result1 = ((Comparable)o1).compareTo(o2); int result2 = ((Comparable)o2).compareTo(o1); // enforce comparable contract if(result1 == 0 && result2 == 0) { return 0; } else if(result1 < 0 && result2 > 0) { return result1; } else if(result1 > 0 && result2 < 0) { return result1; } else { // results inconsistent throw new ClassCastException("o1 not comparable to o2"); } } else { // o2 wasn't comparable throw new ClassCastException( "The first argument of this method was not a Comparable: " + o2.getClass().getName() ); } } else if(o2 instanceof Comparable) { // o1 wasn't comparable throw new ClassCastException( "The second argument of this method was not a Comparable: " + o1.getClass().getName() ); } else { // neither were comparable throw new ClassCastException( "Both arguments of this method were not Comparables: " + o1.getClass().getName() + " and " + o2.getClass().getName() ); } }
/** * Complete the initialization of the package and its meta-model. This method is guarded to have no affect on any * invocation but its first. <!-- begin-user-doc --> <!-- end-user-doc --> * * @generated */ public void initializePackageContents() { if (isInitialized) return; isInitialized = true; // Initialize package setName(eNAME); setNsPrefix(eNS_PREFIX); setNsURI(eNS_URI); // Create type parameters addETypeParameter(comparableEClass, "E"); // Set bounds for type parameters // Add supertypes to classes EGenericType g1 = createEGenericType(this.getComparable()); EGenericType g2 = createEGenericType(this.getCountry()); g1.getETypeArguments().add(g2); countryEClass.getEGenericSuperTypes().add(g1); g1 = createEGenericType(this.getComparable()); g2 = createEGenericType(this.getCity()); g1.getETypeArguments().add(g2); cityEClass.getEGenericSuperTypes().add(g1); g1 = createEGenericType(this.getComparable()); g2 = createEGenericType(this.getCoordinates()); g1.getETypeArguments().add(g2); coordinatesEClass.getEGenericSuperTypes().add(g1); // Initialize classes, features, and operations; add parameters initEClass(countryEClass, Country.class, "Country", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS); initEAttribute(getCountry_Name(), ecorePackage.getEString(), "name", null, 0, 1, Country.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED); initEReference(getCountry_Cities(), this.getCity(), this.getCity_Country(), "cities", null, 0, -1, Country.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED); initEAttribute(getCountry_Code(), ecorePackage.getEString(), "code", null, 0, 1, Country.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED); initEClass(cityEClass, City.class, "City", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS); initEAttribute(getCity_Name(), ecorePackage.getEString(), "name", null, 0, 1, City.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED); initEReference(getCity_Country(), this.getCountry(), this.getCountry_Cities(), "country", null, 0, 1, City.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED); initEReference(getCity_Coordinates(), this.getCoordinates(), null, "coordinates", null, 0, 1, City.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_COMPOSITE, IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED); initEAttribute(getCity_Region(), ecorePackage.getEString(), "region", null, 0, 1, City.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED); initEAttribute(getCity_PostalCode(), ecorePackage.getEString(), "postalCode", null, 0, 1, City.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED); initEClass(coordinatesEClass, Coordinates.class, "Coordinates", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS); initEAttribute(getCoordinates_Latitude(), ecorePackage.getEDouble(), "latitude", null, 0, 1, Coordinates.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED); initEAttribute(getCoordinates_Longitude(), ecorePackage.getEDouble(), "longitude", null, 0, 1, Coordinates.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED); initEClass(comparableEClass, Comparable.class, "Comparable", IS_ABSTRACT, IS_INTERFACE, !IS_GENERATED_INSTANCE_CLASS); // Create resource createResource(eNS_URI); }
/** * Compares two elements by the set comparator, or by natural ordering. * Package visible for use by nested classes. * * @param o1 the first object * @param o2 the second object * @throws ClassCastException if o1 and o2 are not mutually comparable, * or are not Comparable with natural ordering * @throws NullPointerException if o1 or o2 is null with natural ordering */ final int compare(K o1, K o2) { return (comparator == null ? ((Comparable) o1).compareTo(o2) : comparator.compare(o1, o2)); }
/** * 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() ;
/** * Compare two objects with or without a Comparator. If c is null, uses the * natural ordering. Slightly slower than doing it inline if the JVM isn't * clever, but worth it for removing a duplicate of the search code. * Note: This code is also used in Arrays (for sort as well as search). */ static final <T> int compare(T o1, T o2, Comparator<? super T> c) { return c == null ? ((Comparable) o1).compareTo(o2) : c.compare(o1, o2); }