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.api.domain;
18  
19  import com.atlassian.jira.rest.client.api.AddressableEntity;
20  import com.atlassian.jira.rest.client.api.IdentifiableEntity;
21  import com.atlassian.jira.rest.client.api.NamedEntity;
22  import com.google.common.base.Function;
23  import com.google.common.base.Predicate;
24  import com.google.common.collect.Iterables;
25  
26  import java.util.NoSuchElementException;
27  
28  /**
29   * Helper class for entities.
30   *
31   * @since v1.0
32   */
33  public class EntityHelper {
34  
35  	public static Function<IdentifiableEntity<String>, String> GET_ENTITY_STRING_ID_FUNCTION = new Function<IdentifiableEntity<String>, String>() {
36  		@Override
37  		public String apply(IdentifiableEntity<String> entity) {
38  			return entity.getId();
39  		}
40  	};
41  
42  	public static Iterable<String> toNamesList(Iterable<? extends NamedEntity> items) {
43  		return Iterables.transform(items, new Function<NamedEntity, String>() {
44  			@Override
45  			public String apply(NamedEntity from) {
46  				return from.getName();
47  			}
48  		});
49  	}
50  
51  	public static Iterable<String> toFileNamesList(Iterable<? extends Attachment> attachments) {
52  		return Iterables.transform(attachments, new Function<Attachment, String>() {
53  			@Override
54  			public String apply(Attachment a) {
55  				return a.getFilename();
56  			}
57  		});
58  	}
59  
60  	@SuppressWarnings("unused")
61  	public static <T> Iterable<String> toStringIdList(Iterable<IdentifiableEntity<T>> items) {
62  		return Iterables.transform(items, new Function<IdentifiableEntity<T>, String>() {
63  			@Override
64  			public String apply(IdentifiableEntity<T> from) {
65  				return from.getId() == null ? null : from.getId().toString();
66  			}
67  		});
68  	}
69  
70  	public static <T extends NamedEntity> T findEntityByName(Iterable<T> entities, final String name) {
71  		try {
72  			return Iterables.find(entities, HasNamePredicate.forName(name));
73  		} catch (NoSuchElementException ex) {
74  			throw new NoSuchElementException(String.format("Entity with name \"%s\" not found. Entities: %s", name, entities
75  					.toString()));
76  		}
77  	}
78  
79  	@SuppressWarnings("unused")
80  	public static <T extends IdentifiableEntity<K>, K> T findEntityById(Iterable<T> entities, final K id) {
81  		try {
82  			return Iterables.find(entities, HasIdPredicate.forId(id));
83  		} catch (NoSuchElementException ex) {
84  			throw new NoSuchElementException(String.format("Entity with id \"%s\" not found. Entities: %s", id, entities
85  					.toString()));
86  		}
87  	}
88  
89  	public static <T extends Attachment> T findAttachmentByFileName(Iterable<T> attachments, final String fileName) {
90  		return Iterables.find(attachments, HasFileNamePredicate.forFileName(fileName));
91  	}
92  
93  	public static class HasFileNamePredicate<T extends Attachment> implements Predicate<T> {
94  
95  		private final String fileName;
96  
97  		public static <K extends Attachment> HasFileNamePredicate<K> forFileName(String fileName) {
98  			return new HasFileNamePredicate<K>(fileName);
99  		}
100 
101 		private HasFileNamePredicate(String fileName) {
102 			this.fileName = fileName;
103 		}
104 
105 		@Override
106 		public boolean apply(T attachment) {
107 			return fileName.equals(attachment.getFilename());
108 		}
109 	}
110 
111 
112 	public static class HasNamePredicate<T extends NamedEntity> implements Predicate<T> {
113 
114 		private final String name;
115 
116 		public static <K extends NamedEntity> HasNamePredicate<K> forName(String name) {
117 			return new HasNamePredicate<K>(name);
118 		}
119 
120 		private HasNamePredicate(String name) {
121 			this.name = name;
122 		}
123 
124 		@Override
125 		public boolean apply(T input) {
126 			return name.equals(input.getName());
127 		}
128 	}
129 
130 	public static class HasIdPredicate<T extends IdentifiableEntity<K>, K> implements Predicate<T> {
131 
132 		private final K id;
133 
134 		public static <X extends IdentifiableEntity<Y>, Y> HasIdPredicate<X, Y> forId(Y id) {
135 			return new HasIdPredicate<X, Y>(id);
136 		}
137 
138 		private HasIdPredicate(K id) {
139 			this.id = id;
140 		}
141 
142 		@Override
143 		public boolean apply(T input) {
144 			return id.equals(input.getId());
145 		}
146 	}
147 
148 	public static class AddressEndsWithPredicate implements Predicate<AddressableEntity> {
149 
150 		private final String stringEnding;
151 
152 		public AddressEndsWithPredicate(String stringEnding) {
153 			this.stringEnding = stringEnding;
154 		}
155 
156 		@Override
157 		public boolean apply(final AddressableEntity input) {
158 			return input.getSelf().getPath().endsWith(stringEnding);
159 		}
160 	}
161 }