Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2007 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 
     17 package com.google.inject.internal;
     18 
     19 import com.google.common.base.MoreObjects;
     20 import com.google.common.base.Objects;
     21 import com.google.inject.Binder;
     22 import com.google.inject.Key;
     23 import com.google.inject.spi.BindingTargetVisitor;
     24 import com.google.inject.spi.Dependency;
     25 import com.google.inject.spi.UntargettedBinding;
     26 
     27 final class UntargettedBindingImpl<T> extends BindingImpl<T> implements UntargettedBinding<T> {
     28 
     29   UntargettedBindingImpl(InjectorImpl injector, Key<T> key, Object source) {
     30     super(
     31         injector,
     32         key,
     33         source,
     34         new InternalFactory<T>() {
     35           @Override
     36           public T get(InternalContext context, Dependency<?> dependency, boolean linked) {
     37             throw new AssertionError();
     38           }
     39         },
     40         Scoping.UNSCOPED);
     41   }
     42 
     43   public UntargettedBindingImpl(Object source, Key<T> key, Scoping scoping) {
     44     super(source, key, scoping);
     45   }
     46 
     47   @Override
     48   public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
     49     return visitor.visit(this);
     50   }
     51 
     52   @Override
     53   public BindingImpl<T> withScoping(Scoping scoping) {
     54     return new UntargettedBindingImpl<T>(getSource(), getKey(), scoping);
     55   }
     56 
     57   @Override
     58   public BindingImpl<T> withKey(Key<T> key) {
     59     return new UntargettedBindingImpl<T>(getSource(), key, getScoping());
     60   }
     61 
     62   @Override
     63   public void applyTo(Binder binder) {
     64     getScoping().applyTo(binder.withSource(getSource()).bind(getKey()));
     65   }
     66 
     67   @Override
     68   public String toString() {
     69     return MoreObjects.toStringHelper(UntargettedBinding.class)
     70         .add("key", getKey())
     71         .add("source", getSource())
     72         .toString();
     73   }
     74 
     75   @Override
     76   public boolean equals(Object obj) {
     77     if (obj instanceof UntargettedBindingImpl) {
     78       UntargettedBindingImpl<?> o = (UntargettedBindingImpl<?>) obj;
     79       return getKey().equals(o.getKey()) && getScoping().equals(o.getScoping());
     80     } else {
     81       return false;
     82     }
     83   }
     84 
     85   @Override
     86   public int hashCode() {
     87     return Objects.hashCode(getKey(), getScoping());
     88   }
     89 }
     90