View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.api;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Objects;
25  import java.util.Set;
26  
27  abstract class ExtensibleEnums {
28  
29      static Language language(String id) {
30          return new DefaultLanguage(id);
31      }
32  
33      static PathScope pathScope(String id, ProjectScope projectScope, DependencyScope... dependencyScopes) {
34          return new DefaultPathScope(id, projectScope, dependencyScopes);
35      }
36  
37      static ProjectScope projectScope(String id) {
38          return new DefaultProjectScope(id);
39      }
40  
41      private static class DefaultExtensibleEnum implements ExtensibleEnum {
42  
43          private final String id;
44  
45          DefaultExtensibleEnum(String id) {
46              this.id = Objects.requireNonNull(id);
47          }
48  
49          public String id() {
50              return id;
51          }
52  
53          @Override
54          public int hashCode() {
55              return id().hashCode();
56          }
57  
58          @Override
59          public boolean equals(Object obj) {
60              return obj != null && getClass() == obj.getClass() && id().equals(((DefaultExtensibleEnum) obj).id());
61          }
62  
63          @Override
64          public String toString() {
65              return getClass().getSimpleName() + "[" + id() + "]";
66          }
67      }
68  
69      private static class DefaultPathScope extends DefaultExtensibleEnum implements PathScope {
70          private final ProjectScope projectScope;
71          private final Set<DependencyScope> dependencyScopes;
72  
73          DefaultPathScope(String id, ProjectScope projectScope, DependencyScope... dependencyScopes) {
74              super(id);
75              this.projectScope = Objects.requireNonNull(projectScope);
76              this.dependencyScopes =
77                      Collections.unmodifiableSet(new HashSet<>(Arrays.asList(Objects.requireNonNull(dependencyScopes))));
78          }
79  
80          @Override
81          public ProjectScope projectScope() {
82              return projectScope;
83          }
84  
85          @Override
86          public Set<DependencyScope> dependencyScopes() {
87              return dependencyScopes;
88          }
89      }
90  
91      private static class DefaultProjectScope extends DefaultExtensibleEnum implements ProjectScope {
92  
93          DefaultProjectScope(String id) {
94              super(id);
95          }
96      }
97  
98      private static class DefaultLanguage extends DefaultExtensibleEnum implements Language {
99  
100         DefaultLanguage(String id) {
101             super(id);
102         }
103     }
104 }