Example #1
0
    /**
     * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
     *     com.thoughtworks.xstream.io.HierarchicalStreamWriter,
     *     com.thoughtworks.xstream.converters.MarshallingContext)
     */
    public void marshal(
        Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
      Quota quota = (Quota) source;
      BigInteger bytes = quota.getBytes();
      StorageUnit unit = StorageUnit.bestFit(bytes);
      BigDecimal value = unit.fromBytes(bytes);

      writer.startNode("value");
      writer.setValue(value.toString());
      writer.endNode();

      writer.startNode("units");
      writer.setValue(unit.toString());
      writer.endNode();
    }
Example #2
0
    /**
     * @see
     *     com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
     *     com.thoughtworks.xstream.converters.UnmarshallingContext)
     */
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

      Quota quota = new Quota();

      reader.moveDown();
      String nodeName = reader.getNodeName();
      Assert.isTrue("value".equals(nodeName));

      String nodevalue = reader.getValue();
      double value = Double.parseDouble(nodevalue);
      reader.moveUp();

      reader.moveDown();
      nodeName = reader.getNodeName();
      Assert.isTrue("units".equals(nodeName));

      nodevalue = reader.getValue();
      StorageUnit unit = StorageUnit.valueOf(nodevalue);
      reader.moveUp();

      quota.setValue(value, unit);
      return quota;
    }