Example #1
0
  /* ------------------------------------------------------------ */
  protected Object convertTo(Class type, Map map) {
    if (type != null && Convertible.class.isAssignableFrom(type)) {
      try {
        Convertible conv = (Convertible) type.newInstance();
        conv.fromJSON(map);
        return conv;
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }

    Convertor convertor = getConvertor(type);
    if (convertor != null) {
      return convertor.fromJSON(map);
    }
    return map;
  }
 public static void main(String[] args) {
   // Create an instance of Convertible
   Convertible marysCar = new Convertible("yellow", 2015, "Mini", "ragtop");
   System.out.println(marysCar);
   // Create an instance of Motorcycle
   Motorcycle joesBike = new Motorcycle("yellow", 1600);
   System.out.println(joesBike);
   // Creates John and Jen's vehicles
   Convertible johnsCar = new Convertible("silver", 2014, "Mini", "ragtop");
   Truck jensTruck = new Truck("red", 2002, 4);
   // Compare instance variabless  The == sign works for that
   boolean isSameColor = (marysCar.getColor() == joesBike.getColor());
   System.out.println("\nMary's car and Joe's Bike the same color is: " + isSameColor);
   // Mary is married to Dave, so they share the same car
   Convertible davesCar = new Convertible();
   davesCar = marysCar;
   System.out.println(davesCar);
   // Dave put some miles on the shared car
   davesCar.changeMileage(15000);
   // Check that Mary and Dave have the same car
   System.out.println("\nMary and Dave have the same car still? " + davesCar.equals(marysCar));
   // Mary went to the service station. Does she need an oil change?
   marysCar.changeOil();
   // Dang, Dave puts the miles on, but Mary changes the oil. Is it still a Vehicle?
   System.out.println("\nIs Mary's car still a Vehicle? " + (marysCar instanceof Vehicles));
   // Test truck
   jensTruck.changeOil();
   System.out.println(jensTruck);
 }
Example #3
0
 /* ------------------------------------------------------------ */
 public void appendJSON(final Appendable buffer, Convertible converter) {
   ConvertableOutput out = new ConvertableOutput(buffer);
   converter.toJSON(out);
   out.complete();
 }
 public int compareTo(Convertible<MethodIdItem> other) {
   return method.compareTo(other.convert());
 }
 public int compareTo(Convertible<FieldIdItem> other) {
   return field.compareTo(other.convert());
 }