blob: 32e0e9368ded7c521930a88fed13aa1fdfb102f2 [file] [log] [blame]
limpbizkit5fb9d922008-10-14 23:35:56 +00001/**
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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
limpbizkit5fb9d922008-10-14 23:35:56 +000018
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.Key;
limpbizkit5fb9d922008-10-14 23:35:56 +000020import 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 */
limpbizkitc489adf2008-11-18 07:01:33 +000027final class WeakKeySet {
limpbizkit5fb9d922008-10-14 23:35:56 +000028
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}