View Javadoc

1   /*
2    * Copyright (C) 2012 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.jira.rest.client.domain;
18  
19  import com.atlassian.jira.rest.client.AddressableEntity;
20  import com.atlassian.jira.rest.client.IdentifiableEntity;
21  import com.atlassian.jira.rest.client.NamedEntity;
22  import com.google.common.base.Function;
23  import com.google.common.base.Predicate;
24  import com.google.common.collect.Iterables;
25  
26  /**
27   * Helper class for entities.
28   *
29   * @since v1.0
30   */
31  public class EntityHelper {
32  
33  	public static Function<IdentifiableEntity<String>,String> GET_ENTITY_STRING_ID_FUNCTION = new Function<IdentifiableEntity<String>, String>() {
34  		@Override
35  		public String apply(IdentifiableEntity<String> entity) {
36  			return entity.getId();
37  		}
38  	};
39  
40  	public static Iterable<String> toNamesList(Iterable<? extends NamedEntity> items) {
41  		return Iterables.transform(items, new Function<NamedEntity, String>() {
42  			@Override
43  			public String apply(NamedEntity from) {
44  				return from.getName();
45  			}
46  		});
47  	}
48  
49  	@SuppressWarnings("unused")
50  	public static <T> Iterable<String> toStringIdList(Iterable<IdentifiableEntity<T>> items) {
51  		return Iterables.transform(items, new Function<IdentifiableEntity<T>, String>() {
52  			@Override
53  			public String apply(IdentifiableEntity<T> from) {
54  				return from.getId() == null ? null : from.getId().toString();
55  			}
56  		});
57  	}
58  
59  	public static <T extends NamedEntity> T findEntityByName(Iterable<T> entities, final String name) {
60  		return Iterables.find(entities, HasNamePredicate.forName(name));
61  	}
62  
63  	@SuppressWarnings("unused")
64  	public static <T extends IdentifiableEntity<K>, K> T findEntityById(Iterable<T> entities, final K id) {
65  		return Iterables.find(entities, HasIdPredicate.forId(id));
66  	}
67  
68  
69  	public static class HasNamePredicate<T extends NamedEntity> implements Predicate<T> {
70  
71  		private final String name;
72  
73  		public static <K extends NamedEntity> HasNamePredicate<K> forName(String name) {
74  			return new HasNamePredicate<K>(name);
75  		}
76  
77  		private HasNamePredicate(String name) {
78  			this.name = name;
79  		}
80  
81  		@Override
82  		public boolean apply(T input) {
83  			return name.equals(input.getName());
84  		}
85  	}
86  
87  	public static class HasIdPredicate<T extends IdentifiableEntity<K>, K> implements Predicate<T> {
88  
89  		private final K id;
90  
91  		public static <X extends IdentifiableEntity<Y>, Y> HasIdPredicate<X, Y> forId(Y id) {
92  			return new HasIdPredicate<X, Y>(id);
93  		}
94  
95  		private HasIdPredicate(K id) {
96  			this.id = id;
97  		}
98  
99  		@Override
100 		public boolean apply(T input) {
101 			return id.equals(input.getId());
102 		}
103 	}
104 
105 	public static class AddressEndsWithPredicate implements Predicate<AddressableEntity> {
106 
107 		private final String stringEnding;
108 
109 		public AddressEndsWithPredicate(String stringEnding) {
110 			this.stringEnding = stringEnding;
111 		}
112 
113 		@Override
114 		public boolean apply(final AddressableEntity input) {
115 			return input.getSelf().getPath().endsWith(stringEnding);
116 		}
117 	}
118 }