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