View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.reflect;
33  
34  import com.google.common.graph.GraphBuilder;
35  import com.google.common.graph.MutableGraph;
36  import com.google.common.reflect.TypeToken;
37  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
38  import java.lang.reflect.Type;
39  import java.util.ArrayList;
40  import java.util.HashMap;
41  import java.util.HashSet;
42  import java.util.List;
43  import java.util.Map;
44  import java.util.Set;
45  import org.spf4j.ds.Graphs;
46  
47  /**
48   * @author Zoltan Farkas
49   */
50  public final class GraphTypeMap<H> implements TypeMap<H> {
51  
52    private final MutableGraph<TypeToken> typeGraph;
53  
54    private final Map<TypeToken, H> handlers;
55  
56    public GraphTypeMap() {
57      this(16);
58    }
59  
60    public GraphTypeMap(final int expectedSize) {
61      typeGraph = GraphBuilder.directed().allowsSelfLoops(false)
62              .expectedNodeCount(expectedSize).build();
63      handlers = new HashMap<>(expectedSize);
64    }
65  
66    @Override
67    public Set<H> getAll(final Type t) {
68      Set<H> result = new HashSet<>(1);
69      TypeToken tt = TypeToken.of(t);
70      MutableGraph<TypeToken> traverseGraph = Graphs.clone(typeGraph);
71      Set<TypeToken> nodes = traverseGraph.nodes();
72      List<TypeToken> nodesToRemove = new ArrayList<>();
73      do {
74        for (TypeToken token : nodes) {
75          if (traverseGraph.inDegree(token) == 0) {
76            nodesToRemove.add(token);
77            if (tt.isSubtypeOf(token)) {
78              result.add(handlers.get(token));
79            }
80          }
81        }
82        for (TypeToken token : nodesToRemove) {
83          if (!traverseGraph.removeNode(token)) {
84            throw new IllegalStateException("Cannot remove " + token + " from " + traverseGraph);
85          }
86        }
87        nodesToRemove.clear();
88        nodes = traverseGraph.nodes();
89      } while (result.isEmpty() && !nodes.isEmpty());
90      return result;
91    }
92  
93  
94    @Override
95    @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
96    public boolean putIfNotPresent(final Type type, final H value) {
97      TypeToken<?> nType = TypeToken.of(type);
98      if (typeGraph.addNode(nType)) {
99        handlers.put(nType, value);
100       for (TypeToken t : typeGraph.nodes()) {
101         if (!nType.equals(t)) {
102           if (nType.isSubtypeOf(t)) {
103             typeGraph.putEdge(nType, t);
104           } else if (t.isSubtypeOf(nType)) {
105             typeGraph.putEdge(t, nType);
106           }
107         }
108       }
109       return true;
110     } else {
111       return false;
112     }
113   }
114 
115   @Override
116   public boolean remove(final Type type) {
117     TypeToken<?> tt = TypeToken.of(type);
118     if (typeGraph.removeNode(tt)) {
119       handlers.remove(tt);
120       return true;
121     } else {
122       return false;
123     }
124   }
125 
126   @Override
127   public String toString() {
128     return "GraphTypeMap{" + "typeGraph=" + typeGraph + ", handlers=" + handlers + '}';
129   }
130 
131   @Override
132   public H getExact(final Type t) {
133     return handlers.get(TypeToken.of(t));
134   }
135 
136 }