| 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.trmk.vcloud_0_8.suppliers; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | import java.util.NoSuchElementException; |
| 25 | |
| 26 | import org.jclouds.config.ValueOfConfigurationKeyOrNull; |
| 27 | import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType; |
| 28 | |
| 29 | import com.google.common.base.Function; |
| 30 | import com.google.common.base.Predicate; |
| 31 | import com.google.common.collect.Iterables; |
| 32 | |
| 33 | /** |
| 34 | * |
| 35 | * @author Adrian Cole |
| 36 | */ |
| 37 | public class OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault implements |
| 38 | Function<Iterable<ReferenceType>, ReferenceType> { |
| 39 | |
| 40 | protected final ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull; |
| 41 | protected final String configurationKey; |
| 42 | protected final Predicate<ReferenceType> defaultSelector; |
| 43 | |
| 44 | public OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault( |
| 45 | ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull, String configurationKey, |
| 46 | Predicate<ReferenceType> defaultSelector) { |
| 47 | this.configurationKey = checkNotNull(configurationKey, "configurationKey"); |
| 48 | this.valueOfConfigurationKeyOrNull = checkNotNull(valueOfConfigurationKeyOrNull, "valueOfConfigurationKeyOrNull"); |
| 49 | this.defaultSelector = checkNotNull(defaultSelector, "defaultSelector"); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public ReferenceType apply(Iterable<ReferenceType> referenceTypes) { |
| 54 | checkNotNull(referenceTypes, "referenceTypes"); |
| 55 | checkArgument(Iterables.size(referenceTypes) > 0, |
| 56 | "No referenceTypes corresponding to configuration key %s present", configurationKey); |
| 57 | if (Iterables.size(referenceTypes) == 1) |
| 58 | return Iterables.getLast(referenceTypes); |
| 59 | String namingPattern = valueOfConfigurationKeyOrNull.apply(configurationKey); |
| 60 | if (namingPattern != null) { |
| 61 | return findReferenceTypeWithNameMatchingPattern(referenceTypes, namingPattern); |
| 62 | } else { |
| 63 | return defaultReferenceType(referenceTypes); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public ReferenceType defaultReferenceType(Iterable<ReferenceType> referenceTypes) { |
| 68 | return Iterables.find(referenceTypes, defaultSelector); |
| 69 | } |
| 70 | |
| 71 | public ReferenceType findReferenceTypeWithNameMatchingPattern(Iterable<ReferenceType> referenceTypes, |
| 72 | String namingPattern) { |
| 73 | try { |
| 74 | return Iterables.find(referenceTypes, new ReferenceTypeNameMatchesPattern(namingPattern)); |
| 75 | } catch (NoSuchElementException e) { |
| 76 | throw new NoSuchElementException(String.format( |
| 77 | "referenceType matching pattern [%s], corresponding to configuration key %s, not in %s", namingPattern, |
| 78 | configurationKey, referenceTypes)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static class ReferenceTypeNameMatchesPattern implements Predicate<ReferenceType> { |
| 83 | |
| 84 | private final String namingPattern; |
| 85 | |
| 86 | public ReferenceTypeNameMatchesPattern(String namingPattern) { |
| 87 | this.namingPattern = checkNotNull(namingPattern, "namingPattern"); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean apply(ReferenceType arg0) { |
| 92 | return arg0.getName().matches(namingPattern); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public String toString() { |
| 97 | return "nameMatchesPattern(" + namingPattern + ")"; |
| 98 | |
| 99 | } |
| 100 | } |
| 101 | } |