Improved: [FB] Package org.apache.ofbiz.base.container

(OFBIZ-9563)

Fixes some diamond operators

ComponentContainer.java
  deleted multiple unnecessary nullchecks
  added nullcheck, because sortedComponentNames could possibly be assigned as 
    null through the method list()

ContainerConfig.java
  deleted multiple unnecessary else-blocks
  deleted two initializations to shorten the code and return the value directly

Thanks: Dennis Balkir


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1804636 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2017-08-10 07:10:55 +00:00
parent 75c40fef71
commit d0dd85d890
2 changed files with 28 additions and 37 deletions
@@ -53,7 +53,7 @@ public class ComponentContainer implements Container {
private String name;
private final AtomicBoolean loaded = new AtomicBoolean(false);
private final List<Classpath> componentsClassPath = new ArrayList<Classpath>();
private final List<Classpath> componentsClassPath = new ArrayList<>();
@Override
public void init(List<StartupCommand> ofbizCommands, String name, String configFile) throws ContainerException {
@@ -89,7 +89,7 @@ public class ComponentContainer implements Container {
* @throws ContainerException
*/
private void loadClassPathForAllComponents(List<Classpath> componentsClassPath) throws ContainerException {
List<URL> allComponentUrls = new ArrayList<URL>();
List<URL> allComponentUrls = new ArrayList<>();
for(Classpath classPath : componentsClassPath) {
try {
allComponentUrls.addAll(Arrays.asList(classPath.getUrls()));
@@ -137,7 +137,7 @@ public class ComponentContainer implements Container {
File directoryPath = FileUtil.getFile(directoryName);
if (directoryPath.exists() && directoryPath.isDirectory()) {
File componentLoadFile = new File(directoryPath, ComponentLoaderConfig.COMPONENT_LOAD_XML_FILENAME);
if (componentLoadFile != null && componentLoadFile.exists()) {
if (componentLoadFile.exists()) {
loadComponentsInDirectoryUsingLoadFile(directoryPath, componentLoadFile);
} else {
loadComponentsInDirectory(directoryPath);
@@ -162,10 +162,8 @@ public class ComponentContainer implements Container {
try {
configUrl = componentLoadFile.toURI().toURL();
List<ComponentLoaderConfig.ComponentDef> componentsToLoad = ComponentLoaderConfig.getComponentsFromConfig(configUrl);
if (componentsToLoad != null) {
for (ComponentLoaderConfig.ComponentDef def: componentsToLoad) {
loadComponentFromConfig(directoryPath.toString(), def);
}
for (ComponentLoaderConfig.ComponentDef def: componentsToLoad) {
loadComponentFromConfig(directoryPath.toString(), def);
}
} catch (MalformedURLException e) {
Debug.logError(e, "Unable to locate URL for component loading file: " + componentLoadFile.getAbsolutePath(), module);
@@ -184,6 +182,9 @@ public class ComponentContainer implements Container {
*/
private void loadComponentsInDirectory(File directoryPath) throws IOException {
String[] sortedComponentNames = directoryPath.list();
if (sortedComponentNames == null) {
throw new IllegalArgumentException("sortedComponentNames is null, directory path is invalid " + directoryPath.getPath());
}
Arrays.sort(sortedComponentNames);
for (String componentName: sortedComponentNames) {
@@ -47,7 +47,7 @@ public class ContainerConfig {
public static final String module = ContainerConfig.class.getName();
private static Map<String, Configuration> configurations = new LinkedHashMap<String, Configuration>();
private static Map<String, Configuration> configurations = new LinkedHashMap<>();
public static Configuration getConfiguration(String containerName, String configFile) throws ContainerException {
Configuration configuration = configurations.get(containerName);
@@ -89,23 +89,19 @@ public class ContainerConfig {
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value)) {
return defaultValue;
} else {
return prop.value;
}
return prop.value;
}
public static int getPropertyValue(ContainerConfig.Configuration parentProp, String name, int defaultValue) {
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value)) {
return defaultValue;
} else {
int num = defaultValue;
try {
num = Integer.parseInt(prop.value);
} catch (Exception e) {
return defaultValue;
}
return num;
}
try {
return Integer.parseInt(prop.value);
} catch (Exception e) {
return defaultValue;
}
}
@@ -113,32 +109,27 @@ public class ContainerConfig {
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value)) {
return defaultValue;
} else {
return "true".equalsIgnoreCase(prop.value);
}
return "true".equalsIgnoreCase(prop.value);
}
public static String getPropertyValue(ContainerConfig.Configuration.Property parentProp, String name, String defaultValue) {
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value)) {
return defaultValue;
} else {
return prop.value;
}
return prop.value;
}
public static int getPropertyValue(ContainerConfig.Configuration.Property parentProp, String name, int defaultValue) {
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value)) {
return defaultValue;
} else {
int num = defaultValue;
try {
num = Integer.parseInt(prop.value);
} catch (Exception e) {
return defaultValue;
}
return num;
}
try {
return Integer.parseInt(prop.value);
} catch (Exception e) {
return defaultValue;
}
}
@@ -146,9 +137,8 @@ public class ContainerConfig {
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value)) {
return defaultValue;
} else {
return "true".equalsIgnoreCase(prop.value);
}
return "true".equalsIgnoreCase(prop.value);
}
private static Collection<Configuration> getConfigurationPropsFromXml(URL xmlUrl) throws ContainerException {
@@ -159,7 +149,7 @@ public class ContainerConfig {
throw new ContainerException("Error reading the container config file: " + xmlUrl, e);
}
Element root = containerDocument.getDocumentElement();
List<Configuration> result = new ArrayList<Configuration>();
List<Configuration> result = new ArrayList<>();
for (Element curElement: UtilXml.childElementList(root, "container")) {
result.add(new Configuration(curElement));
}
@@ -177,7 +167,7 @@ public class ContainerConfig {
this.className = element.getAttribute("class");
this.loaders = StringUtil.split(element.getAttribute("loaders"), ",");
properties = new LinkedHashMap<String, Property>();
properties = new LinkedHashMap<>();
for (Element curElement: UtilXml.childElementList(element, "property")) {
Property property = new Property(curElement);
properties.put(property.name, property);
@@ -189,7 +179,7 @@ public class ContainerConfig {
}
public List<Property> getPropertiesWithValue(String value) {
List<Property> props = new LinkedList<Property>();
List<Property> props = new LinkedList<>();
if (UtilValidate.isNotEmpty(properties)) {
for (Property p: properties.values()) {
if (p != null && value.equals(p.value)) {
@@ -212,7 +202,7 @@ public class ContainerConfig {
this.value = UtilXml.childElementValue(element, "property-value");
}
properties = new LinkedHashMap<String, Property>();
properties = new LinkedHashMap<>();
for (Element curElement: UtilXml.childElementList(element, "property")) {
Property property = new Property(curElement);
properties.put(property.name, property);
@@ -224,7 +214,7 @@ public class ContainerConfig {
}
public List<Property> getPropertiesWithValue(String value) {
List<Property> props = new LinkedList<Property>();
List<Property> props = new LinkedList<>();
if (UtilValidate.isNotEmpty(properties)) {
for (Property p: properties.values()) {
if (p != null && value.equals(p.value)) {