예제 #1
0
  public void registerWith(ConversionRegistry registry) {

    registry.register(
        Date.class,
        String.class,
        new FinalConvertlet<Date, String>() {
          public String convert(Date from) {
            return DateHelper.formatDateTime((Date) from);
          };
        });

    registry.register(
        String.class,
        Date.class,
        new Convertlet<String, Date>() {
          public Date convert(String from) throws ConvertletException {
            String stringValue = from.trim();
            if (stringValue.length() == 0) {
              return null;
            } else {
              try {
                return DateHelper.parseDateTime(stringValue);
              } catch (ParseException e) {
                throw new ConvertletException(e);
              }
            }
          };
        });

    registry.register(
        Long.class,
        Date.class,
        new FinalConvertlet<Long, Date>() {
          public Date convert(Long from) {
            return new Date(from.longValue());
          };
        });

    registry.register(
        Date.class,
        Long.class,
        new Convertlet<Date, Long>() {
          public Long convert(Date from) {
            return new Long(from.getTime());
          }
        });
  }
예제 #2
0
    public void registerWith(ConversionRegistry registry) {

      registry.register(
          String.class,
          Colours.class,
          new Convertlet<String, Colours>() {
            public Colours convert(String from) throws ConvertletException {

              return Colours.valueOf(from);
            }
          });
      registry.register(
          ColourType.class,
          Colour.class,
          new Convertlet<ColourType, Colour>() {

            public Colour convert(ColourType from) throws ConvertletException {
              switch (from.colour) {
                case GREEN:
                  return new Colour() {
                    public boolean isShiny() {
                      return false;
                    }

                    @Override
                    public String toString() {
                      return "Green";
                    }
                  };
                case RED:
                  return new Colour() {
                    public boolean isShiny() {
                      return true;
                    }

                    @Override
                    public String toString() {
                      return "Red";
                    }
                  };
                default:
                  throw new IllegalStateException("Unrecognized Colour");
              }
            }
          });
    }