Skip to content

Commit

Permalink
SO-1974 moved guavafunctionals to interface for the sake of singletoness
Browse files Browse the repository at this point in the history
  • Loading branch information
Endre Kovacs committed Jun 15, 2016
1 parent 130b9f1 commit 4840e74
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.b2international.snowowl.core.ApplicationContext.getServiceForClass;
import static com.b2international.snowowl.datastore.BranchPathUtils.isMain;
import static com.b2international.snowowl.datastore.ICodeSystem.TO_BRANCH_PATH_FUNCTION;
import static com.google.common.base.Strings.nullToEmpty;

import java.util.Comparator;
Expand Down Expand Up @@ -56,33 +57,6 @@ public class CodeSystemUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(CodeSystemUtils.class);

/**
* Predicate for selecting those {@link ICodeSystem}s:
* <li/> that are residing in the repository denoted by the given repositoryUuid
* <li/> whose branch path matches the active branch's path.
*
* Note: if the active branch is a task branch, this predicate will ignore the last segment (task id part) of the active branch path.
*/
private static final class ActiveCodeSystemPredicate implements Predicate<ICodeSystem> {

private final IBranchPathMap branchPathMap;
private final String repositoryUuid;

private ActiveCodeSystemPredicate(IBranchPathMap branchPathMap, String repositoryUuid) {
this.branchPathMap = branchPathMap;
this.repositoryUuid = repositoryUuid;
}

@Override
public boolean apply(ICodeSystem input) {
IBranchPath activeBranchPath = branchPathMap.getBranchPath(repositoryUuid);
//ignore last segment if we are on a task branch
if (getServiceForClass(TaskManager.class).hasActiveTask())
return activeBranchPath.getParentPath().equals(input.getBranchPath());

return activeBranchPath.getPath().equals(input.getBranchPath());
}
}

private static final class SameRepositoryCodeSystemPredicate implements Predicate<ICodeSystem> {

Expand All @@ -98,53 +72,7 @@ public boolean apply(ICodeSystem input) {
}
}

/**
* Predicate, which selects code systems, that represent the MAIN for its terminology.
* In other words: Predicate, which doesn't let extension code systems through.
*/
private static final class MainCodeSystemPredicate implements Predicate<ICodeSystem> {

@Override
public boolean apply(ICodeSystem input) {
return BranchPathUtils.isMain(input.getBranchPath());
}

}

/**
* Function to turn {@link ICodeSystem} into it's short name.
*/
public static final class CodeSystemToShortNameFunction implements Function<ICodeSystem, String> {

@Override
public String apply(ICodeSystem input) {
return input.getShortName();
}

}

/**
* Function to turn {@link ICodeSystem} into it's repository Uuid.
*/
public static final class CodeSystemToRepositoryUuidFunction implements Function<ICodeSystem, String> {

@Override
public String apply(ICodeSystem input) {
return input.getRepositoryUuid();
}
}


/**
* Function to turn {@link ICodeSystem} into it's branch path string.
*/
public static final class CodeSystemToBranchPathFunction implements Function<ICodeSystem, String> {

@Override
public String apply(ICodeSystem input) {
return input.getBranchPath();
}
}



Expand Down Expand Up @@ -242,7 +170,7 @@ public static ICodeSystem findMatchingCodeSystem(IBranchPath branchPath, String

// branchPath can be: main, task branch, version/tag branch Path, extension branchPath
Iterable<ICodeSystem> codeSystemsInRepositoryUuid = Iterables.filter(getTerminologyRegistryService().getCodeSystems(new UserBranchPathMap()), sameRepositoryCodeSystemPredicate(repositoryUuid));
Map<String, ICodeSystem> branchPathToCodeSystemMap = Maps.uniqueIndex(codeSystemsInRepositoryUuid, new CodeSystemToBranchPathFunction());
Map<String, ICodeSystem> branchPathToCodeSystemMap = Maps.uniqueIndex(codeSystemsInRepositoryUuid, TO_BRANCH_PATH_FUNCTION);

// if (branchPathToCodeSystemMap.containsKey(branchPath.getPath()))
// return branchPathToCodeSystemMap.get(branchPath.getPath());
Expand All @@ -254,7 +182,7 @@ public static ICodeSystem findMatchingCodeSystem(IBranchPath branchPath, String
}

// falling back to the repositoryUUID's main code system.
return Iterables.find(codeSystemsInRepositoryUuid, new MainCodeSystemPredicate());
return Iterables.find(codeSystemsInRepositoryUuid, ICodeSystem.IS_MAIN_BRANCH_PATH_PREDICATE);
}

/**
Expand Down Expand Up @@ -328,26 +256,6 @@ public static Predicate<ICodeSystem> sameRepositoryCodeSystemPredicate(final Str
return new SameRepositoryCodeSystemPredicate(repositoryUUID);
}

public static Predicate<ICodeSystem> activeCodeSystemPredicate(final IBranchPathMap branchPathMap, final String repositoryUuid) {
return new ActiveCodeSystemPredicate(branchPathMap, repositoryUuid);
}

public static Predicate<ICodeSystem> mainCodeSystemPredicate() {
return new MainCodeSystemPredicate();
}

public static Function<ICodeSystem, String> toShortNameFunction() {
return new CodeSystemToShortNameFunction();
}

public static Function<ICodeSystem, String> toRepositoryUuidFunction() {
return new CodeSystemToRepositoryUuidFunction();
}

public static Function<ICodeSystem, String> toBranchPathFunction() {
return new CodeSystemToBranchPathFunction();
}

/*returns with the connection for the given repository UUID*/
private static ICDOConnection getConnection(final String repositoryUuid) {
return getConnectionManager().getByUuid(repositoryUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import javax.annotation.Nullable;

import com.b2international.snowowl.core.api.IBranchPath;
import com.google.common.base.Function;
import com.google.common.base.Predicate;

/**
* Serializable representation of a code system.
*/
Expand Down Expand Up @@ -91,5 +95,65 @@ public interface ICodeSystem extends Serializable {
* @return the path for the code system.
*/
String getBranchPath();


/**
* Predicate selecting code systems representing the MAIN of its terminology.
* In other words: Predicate, which doesn't let <code>extension</code> code systems through.
*/
public static final Predicate<ICodeSystem> IS_MAIN_BRANCH_PATH_PREDICATE = new Predicate<ICodeSystem> () {

@Override
public boolean apply(ICodeSystem input) {
return BranchPathUtils.isMain(input.getBranchPath());
}

};

/**
* Function to turn a {@link ICodeSystem} into it's short name.
*/
public static final Function<ICodeSystem, String> TO_SHORTNAME_FUNCTION = new Function<ICodeSystem, String> () {

@Override
public String apply(ICodeSystem input) {
return input.getShortName();
}

};

/**
* Function to turn a {@link ICodeSystem} into it's repository UUID.
*/
public static final Function<ICodeSystem, String> TO_REPOSITORY_UUID_FUNCTION = new Function<ICodeSystem, String> () {

@Override
public String apply(ICodeSystem input) {
return input.getRepositoryUuid();
}
};


/**
* Function to turn {@link ICodeSystem} into it's branch path string.
*/
public static final Function<ICodeSystem, String> TO_BRANCH_PATH_FUNCTION = new Function<ICodeSystem, String> () {

@Override
public String apply(ICodeSystem input) {
return input.getBranchPath();
}
};

/**
* Function to turn {@link ICodeSystem} into it's {@link IBranchPath}.
*/
public static final Function<ICodeSystem, String> TO_IBRANCH_PATH_FUNCTION = new Function<ICodeSystem, String> () {

This comment has been minimized.

Copy link
@cmark

cmark Jun 15, 2016

Member

Isn't this and the one above the same?

This comment has been minimized.

Copy link
@ekovacs

ekovacs Jun 15, 2016

yes, you are right.
it was supposed to be a ICS => IBranchPath one.
corrected.


@Override
public String apply(ICodeSystem input) {
return input.getBranchPath();
}
};

}

0 comments on commit 4840e74

Please sign in to comment.