limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 17 | package com.google.inject.internal; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 18 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 19 | import com.google.inject.Key; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 20 | import java.util.Set; |
| 21 | |
| 22 | /** |
| 23 | * Minimal set that doesn't hold strong references to the contained keys. |
| 24 | * |
| 25 | * @author jessewilson@google.com (Jesse Wilson) |
| 26 | */ |
limpbizkit | c489adf | 2008-11-18 07:01:33 +0000 | [diff] [blame] | 27 | final class WeakKeySet { |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * We store strings rather than keys so we don't hold strong references. |
| 31 | * |
| 32 | * <p>One potential problem with this approach is that parent and child injectors cannot define |
| 33 | * keys whose class names are equal but class loaders are different. This shouldn't be an issue |
| 34 | * in practice. |
| 35 | */ |
| 36 | private Set<String> backingSet = Sets.newHashSet(); |
| 37 | |
| 38 | public boolean add(Key<?> key) { |
| 39 | return backingSet.add(key.toString()); |
| 40 | } |
| 41 | |
| 42 | public boolean contains(Object o) { |
| 43 | return o instanceof Key && backingSet.contains(o.toString()); |
| 44 | } |
| 45 | } |