Skip to content
Snippets Groups Projects
Commit 92a117e6 authored by Lars Johansson's avatar Lars Johansson
Browse files

Refactor to reduce complexity

parent e3c48613
No related branches found
No related tags found
No related merge requests found
......@@ -58,6 +58,7 @@ import org.openepics.names.util.HolderSystemDeviceStructure;
import org.openepics.names.util.NameElementUtil;
import org.openepics.names.util.NameUtil;
import org.openepics.names.util.TextUtil;
import org.openepics.names.util.Utilities;
import org.openepics.names.util.ValidateNameElementUtil;
import org.openepics.names.util.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -144,7 +145,7 @@ public class NamesService {
// return
// name elements for created names
LOGGER.log(Level.FINE, "createNames, nameElements.size: {0}", String.valueOf(nameElements != null ? nameElements.size() : "null"));
LOGGER.log(Level.FINE, "createNames, nameElements.size: {0}", Utilities.getSize(nameElements));
// initiate holder of containers for system and device structure content, for performance reasons
HolderSystemDeviceStructure holder = new HolderSystemDeviceStructure(holderIRepositories);
......@@ -619,7 +620,7 @@ public class NamesService {
// return
// name elements for updated names
LOGGER.log(Level.FINE, "updateNames, nameElements.size: {0}", String.valueOf(nameElements != null ? nameElements.size() : "null"));
LOGGER.log(Level.FINE, "updateNames, nameElements.size: {0}", Utilities.getSize(nameElements));
// initiate holder of containers for system and device structure content, for performance reasons
HolderSystemDeviceStructure holder = new HolderSystemDeviceStructure(holderIRepositories);
......@@ -699,7 +700,7 @@ public class NamesService {
// return
// name element for deleted name
LOGGER.log(Level.FINE, "deleteNames, nameElements.size: {0}", String.valueOf(nameElements != null ? nameElements.size() : "null"));
LOGGER.log(Level.FINE, "deleteNames, nameElements.size: {0}", Utilities.getSize(nameElements));
// initiate holder of containers for system and device structure content, for performance reasons
HolderSystemDeviceStructure holder = new HolderSystemDeviceStructure(holderIRepositories);
......
......@@ -59,6 +59,7 @@ import org.openepics.names.util.HolderRepositories;
import org.openepics.names.util.HolderSystemDeviceStructure;
import org.openepics.names.util.StructureElementUtil;
import org.openepics.names.util.TextUtil;
import org.openepics.names.util.Utilities;
import org.openepics.names.util.ValidateStructureElementUtil;
import org.openepics.names.util.StructureElementUtil.StructureChoice;
import org.openepics.names.util.ValidateUtil;
......@@ -147,7 +148,7 @@ public class StructuresService {
// return
// structure elements for created structures
LOGGER.log(Level.FINE, "createStructures, structureElements.size: {0}", String.valueOf(structureElements != null ? structureElements.size() : "null"));
LOGGER.log(Level.FINE, "createStructures, structureElements.size: {0}", Utilities.getSize(structureElements));
// initiate holder of containers for system and device structure content, for performance reasons
HolderSystemDeviceStructure holder = new HolderSystemDeviceStructure(holderIRepositories);
......@@ -273,7 +274,7 @@ public class StructuresService {
StructureChoice structureChoice) {
LOGGER.log(Level.FINE, "readStructures, type: {0}", type);
LOGGER.log(Level.FINE, "readStructures, statuses.length: {0}", String.valueOf(statuses != null ? statuses.length : "null"));
LOGGER.log(Level.FINE, "readStructures, statuses.length: {0}", Utilities.getLength(statuses));
LOGGER.log(Level.FINE, "readStructures, deleted: {0}", deleted);
LOGGER.log(Level.FINE, "readStructures, uuid: {0}", uuid);
LOGGER.log(Level.FINE, "readStructures, parentUuid: {0}", parentUuid);
......@@ -288,7 +289,7 @@ public class StructuresService {
LOGGER.log(Level.FINE, "readStructures, offset: {0}", offset);
LOGGER.log(Level.FINE, "readStructures, limit: {0}", limit);
LOGGER.log(Level.FINE, "readStructures, structureChoice: {0}", structureChoice);
if (statuses != null && statuses.length > 0) {
if (statuses != null) {
for (Status status : statuses) {
LOGGER.log(Level.FINE, "readStructures, status: {0}", status);
}
......@@ -406,13 +407,13 @@ public class StructuresService {
// validate input
ValidateUtil.validateInputUuid(uuid);
boolean typeSystemGroup = type != null && Type.SYSTEMGROUP.equals(type);
boolean typeSystem = type != null && Type.SYSTEM.equals(type);
boolean typeSubsystem = type != null && Type.SUBSYSTEM.equals(type);
boolean typeDiscipline = type != null && Type.DISCIPLINE.equals(type);
boolean typeDeviceGroup = type != null && Type.DEVICEGROUP.equals(type);
boolean typeDeviceType = type != null && Type.DEVICETYPE.equals(type);
boolean typeEither = typeSystemGroup || typeSystem || typeSubsystem || typeDiscipline || typeDeviceGroup || typeDeviceType;
boolean typeSystemGroup = Type.SYSTEMGROUP.equals(type);
boolean typeSystem = Type.SYSTEM.equals(type);
boolean typeSubsystem = Type.SUBSYSTEM.equals(type);
boolean typeDiscipline = Type.DISCIPLINE.equals(type);
boolean typeDeviceGroup = Type.DEVICEGROUP.equals(type);
boolean typeDeviceType = Type.DEVICETYPE.equals(type);
boolean typeEither = ValidateUtil.isAnyTrue(typeSystemGroup, typeSystem, typeSubsystem, typeDiscipline, typeDeviceGroup, typeDeviceType);
// do
final List<StructureElement> structureElements = Lists.newArrayList();
......@@ -564,13 +565,13 @@ public class StructuresService {
// validate input
ValidateUtil.validateInputUuid(uuid);
boolean typeSystemGroup = type != null && Type.SYSTEMGROUP.equals(type);
boolean typeSystem = type != null && Type.SYSTEM.equals(type);
boolean typeSubsystem = type != null && Type.SUBSYSTEM.equals(type);
boolean typeDiscipline = type != null && Type.DISCIPLINE.equals(type);
boolean typeDeviceGroup = type != null && Type.DEVICEGROUP.equals(type);
boolean typeDeviceType = type != null && Type.DEVICETYPE.equals(type);
boolean typeEither = typeSystemGroup || typeSystem || typeSubsystem || typeDiscipline || typeDeviceGroup || typeDeviceType;
boolean typeSystemGroup = Type.SYSTEMGROUP.equals(type);
boolean typeSystem = Type.SYSTEM.equals(type);
boolean typeSubsystem = Type.SUBSYSTEM.equals(type);
boolean typeDiscipline = Type.DISCIPLINE.equals(type);
boolean typeDeviceGroup = Type.DEVICEGROUP.equals(type);
boolean typeDeviceType = Type.DEVICETYPE.equals(type);
boolean typeEither = ValidateUtil.isAnyTrue(typeSystemGroup, typeSystem, typeSubsystem, typeDiscipline, typeDeviceGroup, typeDeviceType);
// mnemonic path does not make same sense for history
// (very) tricky to find mnemonic path for uuid at proper time (history)
......@@ -887,7 +888,7 @@ public class StructuresService {
// return
// structure elements for updated structures
LOGGER.log(Level.FINE, "updateStructures, structureElements.size: {0}", String.valueOf(structureElements != null ? structureElements.size() : "null"));
LOGGER.log(Level.FINE, "updateStructures, structureElements.size: {0}", Utilities.getSize(structureElements));
// initiate holder of containers for system and device structure content, for performance reasons
HolderSystemDeviceStructure holder = new HolderSystemDeviceStructure(holderIRepositories);
......@@ -1013,7 +1014,7 @@ public class StructuresService {
// structure elements for deleted structures
// TODO continue ---> validation error
LOGGER.log(Level.FINE, "deleteStructures, structureElements.size: {0}", String.valueOf(structureElements != null ? structureElements.size() : "null"));
LOGGER.log(Level.FINE, "deleteStructures, structureElements.size: {0}", Utilities.getSize(structureElements));
// initiate holder of containers for system and device structure content, for performance reasons
HolderSystemDeviceStructure holder = new HolderSystemDeviceStructure(holderIRepositories);
......@@ -1171,7 +1172,7 @@ public class StructuresService {
// updated structure element
// TODO continue ---> validation error
LOGGER.log(Level.FINE, "approveStructures, structureElements.size: {0}", String.valueOf(structureElements != null ? structureElements.size() : "null"));
LOGGER.log(Level.FINE, "approveStructures, structureElements.size: {0}", Utilities.getSize(structureElements));
// initiate holder of containers for system and device structure content, for performance reasons
HolderSystemDeviceStructure holder = new HolderSystemDeviceStructure(holderIRepositories);
......@@ -1416,7 +1417,7 @@ public class StructuresService {
// updated structure element
// TODO continue ---> validation error
LOGGER.log(Level.FINE, "cancelStructures, structureElements.size: {0}", String.valueOf(structureElements != null ? structureElements.size() : "null"));
LOGGER.log(Level.FINE, "cancelStructures, structureElements.size: {0}", Utilities.getSize(structureElements));
// initiate holder of containers for system and device structure content, for performance reasons
HolderSystemDeviceStructure holder = new HolderSystemDeviceStructure(holderIRepositories);
......@@ -1548,7 +1549,7 @@ public class StructuresService {
// updated structure element
// TODO continue ---> validation error
LOGGER.log(Level.FINE, "rejectStructures, structureElements.size: {0}", String.valueOf(structureElements != null ? structureElements.size() : "null"));
LOGGER.log(Level.FINE, "rejectStructures, structureElements.size: {0}", Utilities.getSize(structureElements));
// initiate holder of containers for system and device structure content, for performance reasons
HolderSystemDeviceStructure holder = new HolderSystemDeviceStructure(holderIRepositories);
......
/*
* Copyright (C) 2022 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.openepics.names.util;
import java.util.Collection;
/**
* Utility class to assist in handling of various things.
*
* @author Lars Johansson
*/
public class Utilities {
/**
* This class is not to be instantiated.
*/
private Utilities() {
throw new IllegalStateException("Utility class");
}
/**
* Return size of collection, <tt>0</tt> if collection is <tt>null</tt>.
*
* @param <T> type parameter
* @param collection collection
* @return size of collection
*/
public static <T> int getSize(Collection<T> collection) {
return collection != null
? collection.size()
: 0;
}
/***
* Return length of array, <tt>0</tt> if array is <tt>null</tt>.
*
* @param <T> type parameter
* @param values values
* @return length of array
*/
@SafeVarargs
public static <T> int getLength(T... values) {
return values != null
? values.length
: 0;
}
}
......@@ -283,7 +283,7 @@ public class ValidateUtil {
* @return boolean if validation is ok
*/
public static boolean isAnyEqual(Type type, Type... values) {
if (isAnyNull(type, values)) {
if (type == null || values == null) {
return false;
}
for (Type value : values) {
......@@ -294,4 +294,22 @@ public class ValidateUtil {
return false;
}
/**
* Validate given values such that <tt>true</tt> is returned if any value is <tt>true</tt>.
*
* @param values values
* @return if validation is ok
*/
public static boolean isAnyTrue(boolean... values) {
if (values == null) {
return false;
}
for (boolean value : values) {
if (value) {
return true;
}
}
return false;
}
}
/*
* Copyright (C) 2022 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.openepics.names.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.openepics.names.rest.beans.Type;
import com.google.common.collect.Lists;
/**
* Unit tests for Utilities class.
*
* @author Lars Johansson
*
* @see Utilities
*/
class UtilitiesTest {
/**
* Test of getting size of collection.
*/
@Test
void getSize() {
assertEquals(0, Utilities.getSize(null));
assertEquals(0, Utilities.getSize(Collections.emptyList()));
assertEquals(0, Utilities.getSize(Collections.emptySet()));
List<String> list = Lists.newArrayList();
assertEquals(0, Utilities.getSize(list));
list.add("a");
assertEquals(1, Utilities.getSize(list));
list.add("b");
list.add("c");
assertEquals(3, Utilities.getSize(list));
list.add("d");
list.add("e");
list.add("f");
assertEquals(6, Utilities.getSize(list));
}
/**
* Test of getting length of array.
*/
@Test
void getLength() {
Object[] values = null;
assertEquals(0, Utilities.getLength(values));
values = new Object[]{"a", 1L};
assertEquals(2, Utilities.getLength(values));
assertEquals(1, Utilities.getLength("a"));
assertEquals(2, Utilities.getLength("a", "b"));
assertEquals(3, Utilities.getLength("a", "b", "c"));
assertEquals(1, Utilities.getLength(1L));
assertEquals(2, Utilities.getLength(1L, 2L));
assertEquals(3, Utilities.getLength(1L, 2L, 3L));
assertEquals(1, Utilities.getLength(Type.SYSTEMGROUP));
assertEquals(1, Utilities.getLength(Type.SYSTEM));
assertEquals(1, Utilities.getLength(Type.SUBSYSTEM));
assertEquals(1, Utilities.getLength(Type.DISCIPLINE));
assertEquals(1, Utilities.getLength(Type.DEVICEGROUP));
assertEquals(1, Utilities.getLength(Type.DEVICETYPE));
assertEquals(2, Utilities.getLength(Type.SYSTEMGROUP, Type.SYSTEM));
assertEquals(3, Utilities.getLength(Type.SYSTEMGROUP, Type.SYSTEM, Type.SUBSYSTEM));
assertEquals(4, Utilities.getLength(Type.SYSTEMGROUP, Type.SYSTEM, Type.SUBSYSTEM, Type.DISCIPLINE));
assertEquals(5, Utilities.getLength(Type.SYSTEMGROUP, Type.SYSTEM, Type.SUBSYSTEM, Type.DISCIPLINE, Type.DEVICEGROUP));
assertEquals(6, Utilities.getLength(Type.SYSTEMGROUP, Type.SYSTEM, Type.SUBSYSTEM, Type.DISCIPLINE, Type.DEVICEGROUP, Type.DEVICETYPE));
assertEquals(2, Utilities.getLength(Type.DEVICETYPE, Type.DEVICEGROUP));
assertEquals(3, Utilities.getLength(Type.DEVICETYPE, Type.DEVICEGROUP, Type.DISCIPLINE));
assertEquals(4, Utilities.getLength(Type.DEVICETYPE, Type.DEVICEGROUP, Type.DISCIPLINE, Type.SUBSYSTEM));
assertEquals(5, Utilities.getLength(Type.DEVICETYPE, Type.DEVICEGROUP, Type.DISCIPLINE, Type.SUBSYSTEM, Type.SYSTEM));
assertEquals(6, Utilities.getLength(Type.DEVICETYPE, Type.DEVICEGROUP, Type.DISCIPLINE, Type.SUBSYSTEM, Type.SYSTEM, Type.SYSTEMGROUP));
}
}
......@@ -580,6 +580,29 @@ class ValidateUtilTest {
assertTrue (ValidateUtil.isAnyEqual(type, Type.DEVICETYPE, Type.DEVICEGROUP, Type.DISCIPLINE, Type.SUBSYSTEM, Type.SYSTEM, Type.SYSTEMGROUP));
}
/**
* Test to validate an array if any value in array is true.
*
* @param s string
* @param i integer
* @param f float
* @param expected expected
*/
@ParameterizedTest
@CsvSource(value = {
"false,false,false,false",
"false,false,true, true",
"false,true, false,true",
"false,true, true, true",
"true, false,false,true",
"true, false,true, true",
"true, true, false,true",
"true, true, true, true"
})
void isAnyTrueParameterized(boolean b1, boolean b2, boolean b3, boolean expected) {
assertEquals(expected, ValidateUtil.isAnyTrue(b1, b2, b3));
}
// ----------------------------------------------------------------------------------------------------
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment