001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tang.implementation.java; 020 021import org.apache.reef.tang.implementation.InjectionPlan; 022import org.apache.reef.tang.types.Node; 023 024public final class JavaInstance<T> extends InjectionPlan<T> { 025 private final T instance; 026 027 public JavaInstance(final Node name, final T instance) { 028 super(name); 029 this.instance = instance; 030 } 031 032 @Override 033 public int getNumAlternatives() { 034 return instance == null ? 0 : 1; 035 } 036 037 @Override 038 public String toString() { 039 return getNode() + " = " + instance; 040 } 041 042 @Override 043 public boolean isAmbiguous() { 044 return false; 045 } 046 047 @Override 048 public boolean isInjectable() { 049 return instance != null; 050 } 051 052 public T getInstance() { 053 return instance; 054 } 055 056 public String getInstanceAsString() { 057 return instance.toString(); 058 } 059 060 @Override 061 protected String toAmbiguousInjectString() { 062 throw new IllegalArgumentException("toAmbiguousInjectString called on JavaInstance!" + this.toString()); 063 } 064 065 @Override 066 protected String toInfeasibleInjectString() { 067 return getNode() + " is not bound."; 068 } 069 070 @Override 071 protected boolean isInfeasibleLeaf() { 072 return true; 073 } 074 075 @Override 076 public String toShallowString() { 077 return toString(); 078 } 079}