1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  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
18package java.security.cert;
19
20import java.util.Collection;
21import java.util.Collections;
22
23/**
24 * The parameters to initialize a <i>Collection</i> type {@code CertStore} instance.
25 * <p>
26 * It is used to specify the {@code Collection} where the {@code CertStore} will
27 * retrieve the certificates and CRLs from.
28 */
29public class CollectionCertStoreParameters implements CertStoreParameters {
30    // Default empty and immutable collection.
31    // Used if <code>CollectionCertStoreParameters</code>instance
32    // created by the no arg constructor
33    private static final Collection<?> defaultCollection = Collections.EMPTY_SET;
34    // A <code>Collection</code> of <code>Certificate</code>s
35    // and <code>CRL</code>s
36    private final Collection<?> collection;
37
38    /**
39     * Creates a new {@code CollectionCertStoreParameters} without a collection.
40     * <p>
41     * The default collection is an empty and unmodifiable {@code Collection}.
42     */
43    public CollectionCertStoreParameters() {
44        this.collection = defaultCollection;
45    }
46
47    /**
48     * Creates a new {@code CollectionCertStoreParameters} with the specified
49     * collection.
50     * <p>
51     * The specified collection is not copied and therefore may be modified at
52     * any time.
53     *
54     * @param collection
55     *            the collection where the {@code Certificate}s and {@code CRL}s
56     *            will be retrieved from.
57     * @throws NullPointerException
58     *             if {@code collection is null}.
59     */
60    public CollectionCertStoreParameters(Collection<?> collection) {
61        if (collection == null) {
62            throw new NullPointerException("collection == null");
63        }
64        this.collection = collection;
65    }
66
67    /**
68     * Clones this {@code CollectionCertStoreParameters} instance, but not the
69     * underlying collection.
70     *
71     * @return the cloned instance.
72     */
73    public Object clone() {
74        try {
75            return super.clone();
76        } catch (CloneNotSupportedException e) {
77            return null;
78        }
79    }
80
81    /**
82     * Returns the collection where the {@code Certificate}s and {@code CRL}s
83     * are retrieved from.
84     *
85     * @return the collection where the {@code Certificate}s and {@code CRL}s
86     *         will be retrieved from.
87     */
88    public Collection<?> getCollection() {
89        return collection;
90    }
91
92    /**
93     * Returns the string representation of this instance.
94     *
95     * @return the string representation of this instance.
96     */
97    public String toString() {
98        StringBuilder sb =
99            new StringBuilder("CollectionCertStoreParameters: [\ncollection: ");
100        sb.append(getCollection().toString());
101        sb.append("\n]");
102        return sb.toString();
103    }
104}
105