| 1 | /** | 
| 2 |  * | 
| 3 |  * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> | 
| 4 |  * | 
| 5 |  * ==================================================================== | 
| 6 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
| 7 |  * you may not use this file except in compliance with the License. | 
| 8 |  * 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, software | 
| 13 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
| 14 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 15 |  * See the License for the specific language governing permissions and | 
| 16 |  * limitations under the License. | 
| 17 |  * ==================================================================== | 
| 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 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 | 
| 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 | 
| 85 |     *  | 
| 86 |     * @param ids | 
| 87 |     *           ids of the images | 
| 88 |     * @return predicate | 
| 89 |     */ | 
| 90 |    public static Predicate<Image> idEquals(final String id) { | 
| 91 |       checkNotNull(id, "id must be defined"); | 
| 92 |       return new Predicate<Image>() { | 
| 93 |          @Override | 
| 94 |          public boolean apply(Image image) { | 
| 95 |             return id.equals(image.getId()); | 
| 96 |          } | 
| 97 |   | 
| 98 |          @Override | 
| 99 |          public String toString() { | 
| 100 |             return "idEquals(" + id + ")"; | 
| 101 |          } | 
| 102 |       }; | 
| 103 |    } | 
| 104 |   | 
| 105 |    /** | 
| 106 |     * return true if this is a 64bit image. | 
| 107 |     */ | 
| 108 |    public static Predicate<Image> is64Bit() { | 
| 109 |       return new Is64BitPredicate(); | 
| 110 |    } | 
| 111 |   | 
| 112 |    /** | 
| 113 |     * return everything. | 
| 114 |     */ | 
| 115 |    public static Predicate<Image> any() { | 
| 116 |       return Predicates.<Image> alwaysTrue(); | 
| 117 |    } | 
| 118 |   | 
| 119 | } |