View Javadoc

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.predicates;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  
23  import java.util.Set;
24  
25  import org.jclouds.javax.annotation.Nullable;
26  
27  import org.jclouds.compute.domain.Image;
28  
29  import com.google.common.base.Predicate;
30  import com.google.common.base.Predicates;
31  import com.google.common.collect.Sets;
32  
33  /**
34   * Container for image filters (predicates).
35   * 
36   * This class has static methods that create customized predicates to use with
37   * {@link org.jclouds.compute.ComputeService}.
38   * 
39   * @author Adrian Cole
40   */
41  public class ImagePredicates {
42     private static final class Is64BitPredicate implements Predicate<Image> {
43        @Override
44        public boolean apply(Image image) {
45           return image.getOperatingSystem().is64Bit();
46        }
47  
48        @Override
49        public String toString() {
50           return "is64Bit()";
51        }
52  
53        @Override
54        public boolean equals(@Nullable Object obj) {
55           return (obj instanceof Is64BitPredicate);
56        }
57  
58     }
59  
60     /**
61      * evaluates true if the Image id is in the supplied set
62      * 
63      * @param ids
64      *           ids of the images
65      * @return predicate
66      */
67     public static Predicate<Image> idIn(Iterable<String> ids) {
68        checkNotNull(ids, "ids must be defined");
69        final Set<String> search = Sets.newHashSet(ids);
70        return new Predicate<Image>() {
71           @Override
72           public boolean apply(Image image) {
73              return search.contains(image.getId());
74           }
75  
76           @Override
77           public String toString() {
78              return "idIn(" + search + ")";
79           }
80        };
81     }
82     
83     /**
84      * evaluates true if the Image metadata contains the following values
85      * 
86      * @param key
87      *           key in Image#getUserMetadata
88      * @param value
89      *           value in Image#getUserMetadata
90       * @return predicate
91      */
92     public static Predicate<Image> userMetadataContains(final String key, final String value) {
93        checkNotNull(key, "key must be defined");
94        checkNotNull(value, "value must be defined");
95        return new Predicate<Image>() {
96           @Override
97           public boolean apply(Image image) {
98              return value.equals(image.getUserMetadata().get(key));
99           }
100 
101          @Override
102          public String toString() {
103             return "metadataContains(" + key +", "+value + ")";
104          }
105       };
106    }
107 
108    /**
109     * evaluates true if the Image
110     * 
111     * @param ids
112     *           ids of the images
113     * @return predicate
114     */
115    public static Predicate<Image> idEquals(final String id) {
116       checkNotNull(id, "id must be defined");
117       return new Predicate<Image>() {
118          @Override
119          public boolean apply(Image image) {
120             return id.equals(image.getId());
121          }
122 
123          @Override
124          public String toString() {
125             return "idEquals(" + id + ")";
126          }
127       };
128    }
129 
130    /**
131     * return true if this is a 64bit image.
132     */
133    public static Predicate<Image> is64Bit() {
134       return new Is64BitPredicate();
135    }
136 
137    /**
138     * return everything.
139     */
140    public static Predicate<Image> any() {
141       return Predicates.<Image> alwaysTrue();
142    }
143 
144 }