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;
020
021import org.apache.reef.tang.types.Node;
022
023import java.util.ArrayList;
024import java.util.List;
025
026public class ListInjectionPlan<T> extends InjectionPlan<T> {
027  private final List<InjectionPlan<T>> entries = new ArrayList<>();
028  private final int numAlternatives;
029  private final boolean isAmbiguous;
030  private final boolean isInjectable;
031
032  public ListInjectionPlan(final Node name, final List<InjectionPlan<T>> entries) {
033    super(name);
034    this.entries.addAll(entries);
035    int numAlternativesAccumulator = 1;
036    boolean isAmbiguousAccumulator = false;
037    boolean isInjectableAccumulator = true;
038    for (final InjectionPlan<T> ip : entries) {
039      numAlternativesAccumulator *= ip.getNumAlternatives();
040      isAmbiguousAccumulator |= ip.isAmbiguous();
041      isInjectableAccumulator &= ip.isInjectable();
042    }
043    this.numAlternatives = numAlternativesAccumulator;
044    this.isAmbiguous = isAmbiguousAccumulator;
045    this.isInjectable = isInjectableAccumulator;
046  }
047
048  @Override
049  public int getNumAlternatives() {
050    return numAlternatives;
051  }
052
053  @Override
054  public boolean isAmbiguous() {
055    return isAmbiguous;
056  }
057
058  @Override
059  public boolean isInjectable() {
060    return isInjectable;
061  }
062
063  public List<InjectionPlan<T>> getEntryPlans() {
064    return new ArrayList<>(this.entries);
065  }
066
067  @Override
068  protected String toAmbiguousInjectString() {
069    final StringBuilder sb = new StringBuilder(getNode().getFullName() + "(list) includes ambiguous plans [");
070    for (final InjectionPlan<T> ip : entries) {
071      if (ip.isAmbiguous()) {
072        sb.append("\n" + ip.toAmbiguousInjectString());
073      }
074    }
075    sb.append("]");
076    return sb.toString();
077  }
078
079  @Override
080  protected String toInfeasibleInjectString() {
081    final StringBuilder sb = new StringBuilder(getNode().getFullName() + "(list) includes infeasible plans [");
082    for (final InjectionPlan<T> ip : entries) {
083      if (!ip.isFeasible()) {
084        sb.append("\n" + ip.toInfeasibleInjectString());
085      }
086    }
087    sb.append("\n]");
088    return sb.toString();
089  }
090
091  @Override
092  protected boolean isInfeasibleLeaf() {
093    return false;
094  }
095
096  @Override
097  public String toShallowString() {
098    final StringBuilder sb = new StringBuilder("list { ");
099    for (final InjectionPlan<T> ip : entries) {
100      sb.append("\n").append(ip.toShallowString());
101    }
102    sb.append("\n } ");
103    return sb.toString();
104  }
105
106}