001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2017 MicroBean.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
014 * implied.  See the License for the specific language governing
015 * permissions and limitations under the License.
016 */
017package org.microbean.helm.chart;
018
019import java.io.Serializable;
020
021import java.util.Collection;
022import java.util.Collections;
023
024import org.microbean.helm.chart.Requirements.Dependency;
025
026/**
027 * A {@link ChartException} indicating that a Helm chart contained a
028 * {@code requirements.yaml} resource, but did not contain at least
029 * one subchart referenced by that resource.
030 *
031 * @author <a href="https://about.me/lairdnelson"
032 * target="_parent">Laird Nelson</a>
033 */
034public class MissingDependenciesException extends ChartException {
035
036
037  /*
038   * Static fields.
039   */
040
041
042  /**
043   * The platform's line separator.
044   */
045  private static final String LS = System.getProperty("line.separator", "\n");
046
047  /**
048   * The version of this class for {@linkplain Serializable
049   * serialization purposes}.
050   */
051  private static final long serialVersionUID = 1L;
052
053
054  /*
055   * Instance fields.
056   */
057
058
059  /**
060   * The {@link Dependency} instances representing the entries within
061   * a {@code requirements.yaml} resource that did not select any
062   * subcharts.
063   *
064   * <p>This field will never be {@code null}.</p>
065   *
066   * @see #getMissingDependencies()
067   */
068  private final Collection<? extends Dependency> missingDependencies;
069
070
071  /*
072   * Constructors.
073   */
074
075
076  /**
077   * Creates a new {@link MissingDependenciesException}.
078   */
079  public MissingDependenciesException() {
080    super();
081    this.missingDependencies = Collections.emptySet();
082  }
083
084  /**
085   * Creates a new {@link MissingDependenciesException}.
086   *
087   * @param missingDependencies a {@link Collection} of {@link
088   * Dependency} instances identifying subcharts that were not found;
089   * may be {@code null}
090   *
091   * @see #getMissingDependencies()
092   */
093  public MissingDependenciesException(final Collection<? extends Dependency> missingDependencies) {
094    super(createMessage(missingDependencies));
095    if (missingDependencies == null) {
096      this.missingDependencies = Collections.emptySet();
097    } else {
098      this.missingDependencies = Collections.unmodifiableCollection(missingDependencies);
099    }
100  }
101
102  /**
103   * Creates a new {@link MissingDependenciesException}.
104   *
105   * @param message a descriptive message; may be {@code null}
106   */
107  public MissingDependenciesException(final String message) {
108    super(message);
109    this.missingDependencies = Collections.emptySet();
110  }
111
112  /**
113   * Creates a new {@link MissingDependenciesException}.
114   *
115   * @param cause the {@link Throwable} responsible for this {@link
116   * MissingDependenciesException}; may be {@code null}
117   */
118  public MissingDependenciesException(final Throwable cause) {
119    super(cause);
120    this.missingDependencies = Collections.emptySet();
121  }
122
123  /**
124   * Creates a new {@link MissingDependenciesException}.
125   *
126   * @param message a descriptive message; may be {@code null}
127   *
128   * @param cause the {@link Throwable} responsible for this {@link
129   * MissingDependenciesException}; may be {@code null}
130   */
131  public MissingDependenciesException(final String message, final Throwable cause) {
132    super(message, cause);
133    this.missingDependencies = Collections.emptySet();
134  }
135
136
137  /*
138   * Instance methods.
139   */
140
141
142  /**
143   * Returns a {@link Collection} of {@link Dependency} instances that
144   * represent entries found in a {@code requirements.yaml} resource
145   * that identify subcharts that were not present.
146   *
147   * <p>This method never returns {@code null}.</p>
148   *
149   * @return a non-{@code null} {@link Collection} of {@link
150   * Dependency} instances that represent entries found in a {@code
151   * requirements.yaml} resource that identify subcharts that were not
152   * present
153   *
154   * @see #MissingDependenciesException(Collection)
155   */
156  public final Collection<? extends Dependency> getMissingDependencies() {
157    return this.missingDependencies;
158  }
159
160
161  /*
162   * Static methods.
163   */
164
165
166  /**
167   * Creates a {@link String} representation of the supplied {@link
168   * Collection} of {@link Dependency} instances.
169   *
170   * <p>This method may return {@code null}.</p>
171   *
172   * @param dependencies the {@link Collection} to represent; may be
173   * {@code null}
174   *
175   * @return a {@link String} representation of the supplied {@code
176   * dependencies}, or {@code null}
177   */
178  private static final String createMessage(final Collection<? extends Dependency> dependencies) {
179    String returnValue = null;
180    if (dependencies != null && !dependencies.isEmpty()) {
181      final StringBuilder sb = new StringBuilder();
182      for (final Dependency dependency : dependencies) {
183        if (dependency != null) {
184          sb.append(dependency).append(LS);
185        }
186      }
187      returnValue = sb.toString();
188    }
189    return returnValue;
190  }
191  
192}