MultipleCryptoFailedException.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.omemo.exceptions;

  18. import java.util.Collections;
  19. import java.util.Iterator;
  20. import java.util.List;

  21. public final class MultipleCryptoFailedException extends CryptoFailedException {

  22.     private static final long serialVersionUID = 1L;

  23.     private final List<CryptoFailedException> cryptoFailedExceptions;

  24.     private MultipleCryptoFailedException(String message, List<CryptoFailedException> cryptoFailedExceptions) {
  25.         super(message);
  26.         this.cryptoFailedExceptions = Collections.unmodifiableList(cryptoFailedExceptions);
  27.         if (cryptoFailedExceptions.isEmpty()) {
  28.             throw new IllegalArgumentException("Exception list must not be empty.");
  29.         }
  30.     }

  31.     public static MultipleCryptoFailedException from(List<CryptoFailedException> cryptoFailedExceptions) {
  32.         StringBuilder sb = new StringBuilder("Multiple CryptoFailedExceptions: ");
  33.         Iterator<CryptoFailedException> it = cryptoFailedExceptions.iterator();
  34.         while (it.hasNext()) {
  35.             sb.append(it.next());
  36.             if (it.hasNext()) {
  37.                 sb.append(", ");
  38.             }
  39.         }
  40.         return new MultipleCryptoFailedException(sb.toString(), cryptoFailedExceptions);
  41.     }

  42.     public List<CryptoFailedException> getCryptoFailedExceptions() {
  43.         return cryptoFailedExceptions;
  44.     }
  45. }