com.atlassian.jira.rest.client.api.domain.EntityHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jira-rest-java-client-api Show documentation
Show all versions of jira-rest-java-client-api Show documentation
The public API for JIRA REST Java Client
/*
* Copyright (C) 2012 Atlassian
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.atlassian.jira.rest.client.api.domain;
import com.atlassian.jira.rest.client.api.AddressableEntity;
import com.atlassian.jira.rest.client.api.IdentifiableEntity;
import com.atlassian.jira.rest.client.api.NamedEntity;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import java.util.NoSuchElementException;
/**
* Helper class for entities.
*
* @since v1.0
*/
public class EntityHelper {
public static Function, String> GET_ENTITY_STRING_ID_FUNCTION = new Function, String>() {
@Override
public String apply(IdentifiableEntity entity) {
return entity.getId();
}
};
public static Function GET_ENTITY_NAME_FUNCTION = new Function() {
@Override
public String apply(NamedEntity entity) {
return entity.getName();
}
};
public static Iterable toNamesList(Iterable extends NamedEntity> items) {
return Iterables.transform(items, GET_ENTITY_NAME_FUNCTION);
}
public static Iterable toFileNamesList(Iterable extends Attachment> attachments) {
return Iterables.transform(attachments, new Function() {
@Override
public String apply(Attachment a) {
return a.getFilename();
}
});
}
@SuppressWarnings("unused")
public static Iterable toStringIdList(Iterable> items) {
return Iterables.transform(items, new Function, String>() {
@Override
public String apply(IdentifiableEntity from) {
return from.getId() == null ? null : from.getId().toString();
}
});
}
public static T findEntityByName(Iterable entities, final String name) {
try {
return Iterables.find(entities, HasNamePredicate.forName(name));
} catch (NoSuchElementException ex) {
throw new NoSuchElementException(String.format("Entity with name \"%s\" not found. Entities: %s", name, entities
.toString()));
}
}
@SuppressWarnings("unused")
public static , K> T findEntityById(Iterable entities, final K id) {
try {
return Iterables.find(entities, HasIdPredicate.forId(id));
} catch (NoSuchElementException ex) {
throw new NoSuchElementException(String.format("Entity with id \"%s\" not found. Entities: %s", id, entities
.toString()));
}
}
public static T findAttachmentByFileName(Iterable attachments, final String fileName) {
return Iterables.find(attachments, HasFileNamePredicate.forFileName(fileName));
}
public static class HasFileNamePredicate implements Predicate {
private final String fileName;
public static HasFileNamePredicate forFileName(String fileName) {
return new HasFileNamePredicate(fileName);
}
private HasFileNamePredicate(String fileName) {
this.fileName = fileName;
}
@Override
public boolean apply(T attachment) {
return fileName.equals(attachment.getFilename());
}
}
public static class HasNamePredicate implements Predicate {
private final String name;
public static HasNamePredicate forName(String name) {
return new HasNamePredicate(name);
}
private HasNamePredicate(String name) {
this.name = name;
}
@Override
public boolean apply(T input) {
return name.equals(input.getName());
}
}
public static class HasIdPredicate, K> implements Predicate {
private final K id;
public static , Y> HasIdPredicate forId(Y id) {
return new HasIdPredicate(id);
}
private HasIdPredicate(K id) {
this.id = id;
}
@Override
public boolean apply(T input) {
return id.equals(input.getId());
}
}
public static class AddressEndsWithPredicate implements Predicate {
private final String stringEnding;
public AddressEndsWithPredicate(String stringEnding) {
this.stringEnding = stringEnding;
}
@Override
public boolean apply(final AddressableEntity input) {
return input.getSelf().getPath().endsWith(stringEnding);
}
}
}