001package io.dinject;
002
003/**
004 * A bean entry in the context with priority and optional name.
005 */
006public class BeanEntry<T> {
007
008  /**
009   * An explicitly supplied bean. See BootContext.
010   */
011  public static final int SUPPLIED = 2;
012
013  /**
014   * An <code>@Primary</code> bean.
015   */
016  public static final int PRIMARY = 1;
017
018  /**
019   * A normal priority bean.
020   */
021  public static final int NORMAL = 0;
022
023  /**
024   * A <code>@Secondary</code> bean.
025   */
026  public static final int SECONDARY = -1;
027
028  private final int priority;
029
030  private final T bean;
031
032  private final String name;
033
034  /**
035   * Construct with priorty, name and the bean.
036   */
037  public BeanEntry(int priority, T bean, String name) {
038    this.priority = priority;
039    this.bean = bean;
040    this.name = name;
041  }
042
043  /**
044   * Return the priority (Primary, Normal and Secondary).
045   */
046  public int getPriority() {
047    return priority;
048  }
049
050  /**
051   * Return the bean.
052   */
053  public T getBean() {
054    return bean;
055  }
056
057  /**
058   * Return the bean name.
059   */
060  public String getName() {
061    return name;
062  }
063
064  /**
065   * Return true if this entry has Supplied priority.
066   */
067  public boolean isSupplied() {
068    return priority == SUPPLIED;
069  }
070
071  /**
072   * Return true if this entry has Primary priority.
073   */
074  public boolean isPrimary() {
075    return priority == PRIMARY;
076  }
077
078  /**
079   * Return true if this entry has Secondary priority.
080   */
081  public boolean isSecondary() {
082    return priority == SECONDARY;
083  }
084
085  @Override
086  public String toString() {
087    StringBuilder sb = new StringBuilder();
088    sb.append("{bean:").append(bean);
089    if (name != null) {
090      sb.append(", name:").append(name);
091    }
092    switch (priority) {
093      case SUPPLIED:
094        sb.append(", Supplied");
095        break;
096      case PRIMARY:
097        sb.append(", @Primary");
098        break;
099      case SECONDARY:
100        sb.append(", @Secondary");
101        break;
102      default:
103        sb.append(", Normal");
104    }
105    return sb.append("}").toString();
106  }
107}