ApplicationBootstrapper.java
01 /*
02  * Copyright 2008-2014 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package griffon.core;
17 
18 import griffon.core.injection.Binding;
19 
20 import javax.annotation.Nonnull;
21 import java.lang.annotation.Annotation;
22 
23 /**
24  @author Andres Almiray
25  @since 2.0.0
26  */
27 public interface ApplicationBootstrapper {
28     void bootstrap() throws Exception;
29 
30     void run();
31 
32     public static class Key {
33         private final Class<?> source;
34         private final Class<? extends Annotation> annotationType;
35         private Annotation annotation;
36 
37         private Key(@Nonnull Class<?> source, @Nonnull Annotation annotation) {
38             this.source = source;
39             this.annotation = annotation;
40             this.annotationType = annotation.getClass();
41         }
42 
43         private Key(@Nonnull Class<?> source, @Nonnull Class<? extends Annotation> annotationType) {
44             this.source = source;
45             this.annotationType = annotationType;
46         }
47 
48         @Override
49         public boolean equals(Object o) {
50             if (this == oreturn true;
51             if (o == null || getClass() != o.getClass()) return false;
52 
53             ApplicationBootstrapper.Key key = (ApplicationBootstrapper.Keyo;
54 
55             if (annotation != null) {
56                 return source.equals(key.source&& annotation.equals(key.annotation);
57             }
58 
59             return source.equals(key.source&&
60                 !(annotationType != null ? !annotationType.equals(key.annotationType: key.annotationType != null);
61         }
62 
63         @Override
64         public int hashCode() {
65             int result = source.hashCode();
66             if (annotation != null) {
67                 result = 31 * result + annotation.hashCode();
68             else {
69                 result = 31 * result + (annotationType != null ? annotationType.hashCode() 0);
70             }
71             return result;
72         }
73 
74         @Override
75         public String toString() {
76             final StringBuilder sb = new StringBuilder("Key{");
77             sb.append("source=").append(source);
78             sb.append(", annotation=").append(annotation);
79             sb.append(", annotationType=").append(annotationType);
80             sb.append('}');
81             return sb.toString();
82         }
83 
84         @Nonnull
85         @SuppressWarnings("ConstantConditions")
86         public static ApplicationBootstrapper.Key of(Binding<?> binding) {
87             return binding.getClassifier() != null new ApplicationBootstrapper.Key(binding.getSource(), binding.getClassifier()) :
88                 new ApplicationBootstrapper.Key(binding.getSource(), binding.getClassifierType());
89         }
90     }
91 }