View Javadoc

1   package org.apache.commons.graph.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.lang.reflect.Proxy.newProxyInstance;
23  
24  import java.lang.reflect.InvocationHandler;
25  import java.lang.reflect.Method;
26  
27  import org.apache.commons.graph.Edge;
28  import org.apache.commons.graph.Vertex;
29  
30  /**
31   * Simple {@link Edge} Proxy that inverts head/tail for undirectedGraph implementations.
32   *
33   * @param <V> the Graph vertices type
34   */
35  final class InvertedEdgeAdapter<V extends Vertex>
36      implements Edge<V>, InvocationHandler
37  {
38  
39      /**
40       * Creates a new inverted {@link Edge}.
41       *
42       * @param <V> the Graph vertices type
43       * @param <E> the Graph edges type
44       * @param edge the edge has to be inverted
45       * @return
46       */
47      public static <V extends Vertex, E extends Edge<V>> E invertHeadAndTail( E edge )
48      {
49          @SuppressWarnings( "unchecked" ) // type driven by input
50          E edgeProxy = (E) newProxyInstance( edge.getClass().getClassLoader(), edge.getClass().getInterfaces(),
51                                              new InvertedEdgeAdapter<V>( edge ) );
52          return edgeProxy;
53      }
54  
55      private final Edge<V> adapted;
56  
57      /**
58       * Creates a new inverted {@link Edge}, wrapping a default one.
59       *
60       * @param adapted the wrapped Edge
61       */
62      private InvertedEdgeAdapter( Edge<V> adapted )
63      {
64          this.adapted = adapted;
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      public V getHead()
71      {
72          return adapted.getTail();
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      public V getTail()
79      {
80          return adapted.getHead();
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      public Object invoke( Object proxy, Method method, Object[] args )
87          throws Throwable
88      {
89          try
90          {
91              return method.invoke( this, args );
92          }
93          catch ( Throwable t )
94          {
95              return method.invoke( adapted, args );
96          }
97      }
98  
99  }