| 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.providers; |
| 20 | |
| 21 | import com.google.common.base.Preconditions; |
| 22 | import com.google.common.base.Predicate; |
| 23 | import com.google.common.base.Predicates; |
| 24 | |
| 25 | import org.jclouds.util.Preconditions2; |
| 26 | |
| 27 | /** |
| 28 | * Container for provider filters (predicates). |
| 29 | * |
| 30 | * @author Jeremy Whitlock <jwhitlock@apache.org> |
| 31 | */ |
| 32 | public class ProviderPredicates { |
| 33 | |
| 34 | /** |
| 35 | * Returns all providers available to jclouds regardless of type. |
| 36 | * |
| 37 | * @return all available providers |
| 38 | */ |
| 39 | public static Predicate<ProviderMetadata> all() { |
| 40 | return Predicates.<ProviderMetadata> alwaysTrue(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns all providers with the given id. |
| 45 | * |
| 46 | * @param id |
| 47 | * the id of the provider to return |
| 48 | * |
| 49 | * @return the providers with the given id |
| 50 | */ |
| 51 | public static Predicate<ProviderMetadata> id(final String id) { |
| 52 | Preconditions2.checkNotEmpty(id, "id must be defined"); |
| 53 | return new Predicate<ProviderMetadata>() { |
| 54 | /** |
| 55 | * {@inheritDoc} |
| 56 | */ |
| 57 | @Override |
| 58 | public boolean apply(ProviderMetadata providerMetadata) { |
| 59 | return providerMetadata.getId().equals(id); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * {@inheritDoc} |
| 64 | */ |
| 65 | @Override |
| 66 | public String toString() { |
| 67 | return "id(" + id + ")"; |
| 68 | } |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns all providers with the given type. |
| 74 | * |
| 75 | * @param type |
| 76 | * the type of the provider to return |
| 77 | * |
| 78 | * @return the providers with the given type |
| 79 | */ |
| 80 | public static Predicate<ProviderMetadata> type(final String type) { |
| 81 | Preconditions2.checkNotEmpty(type, "type must be defined"); |
| 82 | return new Predicate<ProviderMetadata>() { |
| 83 | /** |
| 84 | * {@inheritDoc} |
| 85 | */ |
| 86 | @Override |
| 87 | public boolean apply(ProviderMetadata providerMetadata) { |
| 88 | return providerMetadata.getType().equals(type); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * {@inheritDoc} |
| 93 | */ |
| 94 | @Override |
| 95 | public String toString() { |
| 96 | return "type(" + type + ")"; |
| 97 | } |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns the providers that are bound to the same location as the given ISO 3166 code. |
| 103 | * |
| 104 | * @param isoCode |
| 105 | * the ISO 3166 code to filter providers by |
| 106 | * |
| 107 | * @return the providers with the given ISO 3166 code |
| 108 | */ |
| 109 | public static Predicate<ProviderMetadata> boundedByIso3166Code(final String iso3166Code) { |
| 110 | Preconditions.checkNotNull(iso3166Code, "iso3166Code must not be null"); |
| 111 | |
| 112 | return new Predicate<ProviderMetadata>() { |
| 113 | /** |
| 114 | * {@inheritDoc} |
| 115 | */ |
| 116 | @Override |
| 117 | public boolean apply(ProviderMetadata providerMetadata) { |
| 118 | return providerContainsIso3166Code(providerMetadata, iso3166Code); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * {@inheritDoc} |
| 123 | */ |
| 124 | @Override |
| 125 | public String toString() { |
| 126 | return "boundedByIso3166Code(" + iso3166Code + ")"; |
| 127 | } |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Return all providers that have at least one ISO 3166 code in common with the given provider metadata. |
| 133 | * |
| 134 | * @param refProviderMetadata |
| 135 | * the provider metadata to use to filter providers by |
| 136 | * |
| 137 | * @return the providers that have at least one ISO 3166 code in common |
| 138 | */ |
| 139 | public static Predicate<ProviderMetadata> intersectingIso3166Code(final ProviderMetadata refProviderMetadata) { |
| 140 | Preconditions.checkNotNull(refProviderMetadata, "refProviderMetadata must not be null"); |
| 141 | |
| 142 | return new Predicate<ProviderMetadata>() { |
| 143 | /** |
| 144 | * {@inheritDoc} |
| 145 | */ |
| 146 | @Override |
| 147 | public boolean apply(ProviderMetadata providerMetadata) { |
| 148 | for (String refIso3166Code : refProviderMetadata.getIso3166Codes()) { |
| 149 | // Return only if the potential provider contains the same ISO 3166 code and the provider and |
| 150 | // reference provider are not the same. |
| 151 | if (providerContainsIso3166Code(providerMetadata, refIso3166Code) && |
| 152 | !refProviderMetadata.equals(providerMetadata)) { |
| 153 | return true; |
| 154 | } |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * {@inheritDoc} |
| 161 | */ |
| 162 | @Override |
| 163 | public String toString() { |
| 164 | return "intersectingIso3166Code(" + refProviderMetadata + ")"; |
| 165 | } |
| 166 | }; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Returns whether or not the provided provider contains the ISO 3166 code provider or is within the same |
| 171 | * "global" region, like "US" would contain "US-*". |
| 172 | * |
| 173 | * @param providerMetadata |
| 174 | * the provider metadata to search |
| 175 | * @param iso3166Code |
| 176 | * the ISO 3166 code to search the provider metadata for |
| 177 | * |
| 178 | * @return the result |
| 179 | */ |
| 180 | private static boolean providerContainsIso3166Code(ProviderMetadata providerMetadata, String iso3166Code) { |
| 181 | for (String availCode : providerMetadata.getIso3166Codes()) { |
| 182 | if(iso3166Code.indexOf('-') == -1) { |
| 183 | if (availCode.startsWith(iso3166Code + "-")) { |
| 184 | return true; |
| 185 | } |
| 186 | } else if (availCode.equals(iso3166Code)) { |
| 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return false; |
| 192 | } |
| 193 | } |