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.compute.domain; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.net.URI; |
24 | import java.util.Map; |
25 | import java.util.Set; |
26 | |
27 | import org.jclouds.compute.domain.internal.ComputeMetadataImpl; |
28 | import org.jclouds.domain.Location; |
29 | import org.jclouds.domain.ResourceMetadataBuilder; |
30 | |
31 | import com.google.common.collect.ImmutableSet; |
32 | |
33 | /** |
34 | * |
35 | * @author Adrian Cole |
36 | */ |
37 | public class ComputeMetadataBuilder extends ResourceMetadataBuilder<ComputeType> { |
38 | protected String id; |
39 | protected ComputeType type; |
40 | protected Set<String> tags = ImmutableSet.<String>of(); |
41 | |
42 | public ComputeMetadataBuilder(ComputeType type) { |
43 | this.type = checkNotNull(type, "type"); |
44 | } |
45 | |
46 | public ComputeMetadataBuilder id(String id) { |
47 | this.id = id; |
48 | return this; |
49 | } |
50 | |
51 | public ComputeMetadataBuilder tags(Iterable<String> tags) { |
52 | this.tags = ImmutableSet.<String> copyOf(checkNotNull(tags, "tags")); |
53 | return this; |
54 | } |
55 | |
56 | /** |
57 | * set id and providerId to the same value; |
58 | */ |
59 | public ComputeMetadataBuilder ids(String id) { |
60 | id(id).providerId(id); |
61 | return this; |
62 | } |
63 | |
64 | @Override |
65 | public ComputeMetadataBuilder providerId(String providerId) { |
66 | return ComputeMetadataBuilder.class.cast(super.providerId(providerId)); |
67 | } |
68 | |
69 | @Override |
70 | public ComputeMetadataBuilder name(String name) { |
71 | return ComputeMetadataBuilder.class.cast(super.name(name)); |
72 | } |
73 | |
74 | @Override |
75 | public ComputeMetadataBuilder location(Location location) { |
76 | return ComputeMetadataBuilder.class.cast(super.location(location)); |
77 | } |
78 | |
79 | @Override |
80 | public ComputeMetadataBuilder uri(URI uri) { |
81 | return ComputeMetadataBuilder.class.cast(super.uri(uri)); |
82 | } |
83 | |
84 | @Override |
85 | public ComputeMetadataBuilder userMetadata(Map<String, String> userMetadata) { |
86 | return ComputeMetadataBuilder.class.cast(super.userMetadata(userMetadata)); |
87 | } |
88 | |
89 | public ComputeMetadata build() { |
90 | return new ComputeMetadataImpl(type, providerId, name, id, location, uri, userMetadata, tags); |
91 | } |
92 | |
93 | public static ComputeMetadataBuilder fromComputeMetadata(ComputeMetadata in) { |
94 | return new ComputeMetadataBuilder(in.getType()).id(in.getId()).location(in.getLocation()).name(in.getName()) |
95 | .uri(in.getUri()).userMetadata(in.getUserMetadata()).tags(in.getTags()); |
96 | } |
97 | } |