1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds 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.jclouds.rest; |
20 | |
21 | import static com.google.common.base.Preconditions.checkArgument; |
22 | import static com.google.common.base.Preconditions.checkNotNull; |
23 | import static java.util.Collections.EMPTY_LIST; |
24 | |
25 | import org.jclouds.PropertiesBuilder; |
26 | |
27 | import com.google.common.base.Objects; |
28 | import com.google.common.collect.ImmutableList; |
29 | import com.google.inject.Module; |
30 | |
31 | /** |
32 | * @author Adrian Cole |
33 | */ |
34 | public class RestContextSpec<S, A> { |
35 | protected final String provider; |
36 | protected final String endpoint; |
37 | protected final String apiVersion; |
38 | protected final String iso3166Codes; |
39 | protected final String identity; |
40 | protected final String credential; |
41 | protected final Class<S> sync; |
42 | protected final Class<A> async; |
43 | protected final Class<PropertiesBuilder> propertiesBuilderClass; |
44 | protected final Class<RestContextBuilder<S, A>> contextBuilderClass; |
45 | protected final Iterable<Module> modules; |
46 | |
47 | public RestContextSpec(String provider, String endpoint, String apiVersion, String iso3166Codes, String identity, |
48 | String credential, Class<S> sync, Class<A> async, Class<PropertiesBuilder> propertiesBuilderClass, |
49 | Class<RestContextBuilder<S, A>> contextBuilderClass, Iterable<Module> modules) { |
50 | this.provider = checkNotNull(provider, "provider"); |
51 | this.endpoint = endpoint; |
52 | this.apiVersion = apiVersion; |
53 | this.identity = identity; |
54 | this.credential = credential; |
55 | this.iso3166Codes = iso3166Codes; |
56 | this.sync = sync; |
57 | this.async = async; |
58 | checkArgument(RestContextBuilder.class.isAssignableFrom(contextBuilderClass), contextBuilderClass.getName() |
59 | + " is not a subclass of " + RestContextBuilder.class.getName()); |
60 | checkArgument(PropertiesBuilder.class.isAssignableFrom(propertiesBuilderClass), propertiesBuilderClass.getName() |
61 | + " is not a subclass of " + PropertiesBuilder.class.getName()); |
62 | this.propertiesBuilderClass = propertiesBuilderClass; |
63 | this.contextBuilderClass = contextBuilderClass; |
64 | this.modules = ImmutableList.copyOf(modules); |
65 | } |
66 | |
67 | @SuppressWarnings( { "unchecked", "rawtypes" }) |
68 | public RestContextSpec(String provider, String endpoint, String apiVersion, String iso3166Codes, String identity, |
69 | String credential, Class<S> sync, Class<A> async) { |
70 | this(provider, endpoint, apiVersion, iso3166Codes, identity, credential, sync, async, PropertiesBuilder.class, |
71 | (Class) RestContextBuilder.class, EMPTY_LIST); |
72 | } |
73 | |
74 | /** |
75 | * this uses the inefficient {@link Objects} implementation as the object count will be |
76 | * relatively small and therefore efficiency is not a concern. |
77 | */ |
78 | @Override |
79 | public int hashCode() { |
80 | return Objects.hashCode(provider, endpoint, apiVersion, iso3166Codes, identity, credential, sync, async, |
81 | propertiesBuilderClass, contextBuilderClass, modules); |
82 | } |
83 | |
84 | /** |
85 | * this uses the inefficient {@link Objects} implementation as the object count will be |
86 | * relatively small and therefore efficiency is not a concern. |
87 | */ |
88 | @Override |
89 | public boolean equals(Object that) { |
90 | if (that == null) |
91 | return false; |
92 | return Objects.equal(this.toString(), that.toString()); |
93 | } |
94 | |
95 | /** |
96 | * this uses the inefficient {@link Objects} implementation as the object count will be |
97 | * relatively small and therefore efficiency is not a concern. |
98 | */ |
99 | @Override |
100 | public String toString() { |
101 | return Objects.toStringHelper(this).add("provider", provider).add("endpoint", endpoint).add("apiVersion", |
102 | apiVersion).add("iso3166Codes", iso3166Codes).add("identity", identity).add("sync", sync).add("async", |
103 | async).add("propertiesBuilderClass", propertiesBuilderClass).add("contextBuilderClass", |
104 | contextBuilderClass).add("modules", modules).toString(); |
105 | } |
106 | |
107 | } |