| 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.softlayer.predicates; |
| 20 | |
| 21 | import com.google.common.base.Predicate; |
| 22 | import org.jclouds.softlayer.domain.ProductItem; |
| 23 | import org.jclouds.softlayer.domain.ProductItemCategory; |
| 24 | |
| 25 | import java.util.regex.Pattern; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | public class ProductItemPredicates { |
| 30 | |
| 31 | /** |
| 32 | * Tests if the ProductItem contains the required category. |
| 33 | * |
| 34 | * @param category |
| 35 | * @return true if it does, otherwise false. |
| 36 | */ |
| 37 | public static Predicate<ProductItem> categoryCode(final String category) { |
| 38 | checkNotNull(category, "category cannot be null"); |
| 39 | return new Predicate<ProductItem>() { |
| 40 | @Override |
| 41 | public boolean apply(ProductItem productItem) { |
| 42 | checkNotNull(productItem, "productItem cannot ne null"); |
| 43 | for (ProductItemCategory productItemCategory : productItem.getCategories()) { |
| 44 | if (category.equals(productItemCategory.getCategoryCode())) |
| 45 | return true; |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public String toString() { |
| 52 | return "categoryCode(" + category + ")"; |
| 53 | } |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Tests if the ProductItem contains a category that matches the supplied Pattern |
| 59 | * |
| 60 | * @param category |
| 61 | * @return true if it does, otherwise false. |
| 62 | */ |
| 63 | public static Predicate<ProductItem> categoryCodeMatches(final Pattern category) { |
| 64 | checkNotNull(category, "category cannot be null"); |
| 65 | return new Predicate<ProductItem>() { |
| 66 | @Override |
| 67 | public boolean apply(ProductItem productItem) { |
| 68 | checkNotNull(productItem, "productItem cannot ne null"); |
| 69 | for (ProductItemCategory productItemCategory : productItem.getCategories()) { |
| 70 | if (category.matcher(productItemCategory.getCategoryCode()).matches()) |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String toString() { |
| 78 | return "categoryCodeMatches(" + category + ")"; |
| 79 | } |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Tests if the ProductItem has the required capacity. |
| 85 | * |
| 86 | * @param capacity |
| 87 | * @return true if it does, otherwise false. |
| 88 | */ |
| 89 | public static Predicate<ProductItem> capacity(final Float capacity) { |
| 90 | checkNotNull(capacity, "capacity cannot be null"); |
| 91 | return new Predicate<ProductItem>() { |
| 92 | @Override |
| 93 | public boolean apply(ProductItem productItem) { |
| 94 | checkNotNull(productItem, "productItem cannot ne null"); |
| 95 | Float productItemCapacity = productItem.getCapacity(); |
| 96 | if (productItemCapacity == null) |
| 97 | return false; |
| 98 | return capacity.equals(productItemCapacity); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public String toString() { |
| 103 | return "capacity(" + capacity + ")"; |
| 104 | } |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Tests if the ProductItem has the required units. |
| 110 | * |
| 111 | * @param units |
| 112 | * @return true if it does, otherwise false. |
| 113 | */ |
| 114 | public static Predicate<ProductItem> units(final String units) { |
| 115 | checkNotNull(units, "units cannot be null"); |
| 116 | return new Predicate<ProductItem>() { |
| 117 | @Override |
| 118 | public boolean apply(ProductItem productItem) { |
| 119 | checkNotNull(productItem, "productItem cannot ne null"); |
| 120 | return units.equals(productItem.getUnits()); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public String toString() { |
| 125 | return "units(" + units + ")"; |
| 126 | } |
| 127 | }; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Tests if the ProductItem's description matches the supplied regular expression. |
| 132 | * |
| 133 | * @param regex |
| 134 | * a regular expression to match against. |
| 135 | * @return true if it does, otherwise false. |
| 136 | * @throws java.util.regex.PatternSyntaxException |
| 137 | * if the regex is invalid |
| 138 | */ |
| 139 | public static Predicate<ProductItem> matches(final Pattern regex) { |
| 140 | checkNotNull(regex, "regex cannot be null"); |
| 141 | |
| 142 | return new Predicate<ProductItem>() { |
| 143 | @Override |
| 144 | public boolean apply(ProductItem productItem) { |
| 145 | checkNotNull(productItem, "productItem cannot ne null"); |
| 146 | return regex.matcher(productItem.getDescription()).matches(); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public String toString() { |
| 151 | return "regex(" + regex + ")"; |
| 152 | } |
| 153 | }; |
| 154 | } |
| 155 | } |