Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/phoenixst/plexus/examples/NodeOffsetTransformer.java


1   /*
2    *  $Id: NodeOffsetTransformer.java,v 1.4 2003/10/22 20:48:45 rconner Exp $
3    *
4    *  Copyright (C) 1994-2003 by Phoenix Software Technologists,
5    *  Inc. and others.  All rights reserved.
6    *
7    *  THIS PROGRAM AND DOCUMENTATION IS PROVIDED UNDER THE TERMS OF THE
8    *  COMMON PUBLIC LICENSE ("AGREEMENT") WHICH ACCOMPANIES IT.  ANY
9    *  USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
10   *  RECIPIENT'S ACCEPTANCE OF THE AGREEMENT.
11   *
12   *  The license text can also be found at
13   *    http://opensource.org/licenses/cpl.php
14   */
15  
16  package com.phoenixst.plexus.examples;
17  
18  import org.apache.commons.collections.Transformer;
19  
20  import com.phoenixst.plexus.*;
21  
22  
23  /**
24   *  A {@link Graph} which wraps another, adding the specified offset
25   *  to the underlying graph's nodes, which must be
26   *  <code>Integers</code>.
27   *
28   *  @version    $Revision: 1.4 $
29   *  @author     Ray A. Conner
30   *
31   *  @since      1.0
32   */
33  public class NodeOffsetTransformer extends GraphTransformer
34  {
35  
36      /**
37       *  The number to add to each of the underlying graph's nodes.
38       */
39      private int offset;
40  
41  
42      /**
43       *  Creates a new <code>NodeOffsetTransformer</code>.
44       */
45      public NodeOffsetTransformer( Graph g, int offset )
46      {
47          super( g );
48          this.offset = offset;
49          setNodeTransformer( new IntegerOffsetTransformer() );
50      }
51  
52  
53      public String toString()
54      {
55          StringBuffer s = new StringBuffer( "Offset( " + offset + " ) wrapping " );
56          s.append( g.toString() );
57          return s.toString();
58      }
59  
60  
61      /**
62       *  Transformer which adds an offset to Integers.
63       */
64      private class IntegerOffsetTransformer
65          implements InvertibleTransformer,
66                     java.io.Serializable
67      {
68          public Object transform( Object object )
69          {
70              if( !(object instanceof Integer) ) {
71                  return object;
72              }
73              return new Integer( ((Integer) object).intValue() + offset );
74          }
75  
76          public Object untransform( Object object )
77          {
78              if( !(object instanceof Integer) ) {
79                  return object;
80              }
81              return new Integer( ((Integer) object).intValue() - offset );
82          }
83      }
84  
85  }