Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MCR-3229 enable PMD rule LooseCoupling #2353

Open
wants to merge 2 commits into
base: 2024.06.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class MCRAccessCacheHelper {
public static void clearPermissionCache(String id) {
LOGGER.info("Invalidate all permissions for obj {} from local cache", id);

final ArrayList<String> idsToClear = new ArrayList<>();
List<String> idsToClear = new ArrayList<>();
idsToClear.add(id);
collectDescendants(idsToClear, id);
MCRAccessManager.invalidPermissionCacheByID(idsToClear.toArray(new String[0]));
Expand All @@ -53,7 +53,7 @@ public static void clearPermissionCache(String id) {
public static void clearAllPermissionCaches(String id) {
LOGGER.info("Invalidate all permissions for obj {} from all caches", id);

final ArrayList<String> idsToClear = new ArrayList<>();
List<String> idsToClear = new ArrayList<>();
idsToClear.add(id);
collectDescendants(idsToClear, id);
MCRAccessManager.invalidAllPermissionCachesById(idsToClear.toArray(new String[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -45,7 +46,7 @@
*/
public class MCRAccessControlSystem extends MCRAccessBaseImpl {
private static MCRAccessControlSystem singleton;
private static final HashMap<String, Integer> NEXT_FREE_RULE_ID = new HashMap<>();
private static final Map<String, Integer> NEXT_FREE_RULE_ID = new HashMap<>();

public static final String SYSTEM_RULE_PREFIX = "SYSTEMRULE";

Expand All @@ -61,14 +62,14 @@ public class MCRAccessControlSystem extends MCRAccessBaseImpl {

boolean disabled;

static Hashtable<String, String> ruleIDTable = new Hashtable<>();
static Map<String, String> ruleIDTable = new Hashtable<>();

private static final Logger LOGGER = LogManager.getLogger(MCRAccessControlSystem.class);

private MCRAccessControlSystem() {
String pools = MCRConfiguration2.getString("MCR.Access.AccessPermissions").orElse("read,write,delete");

if (pools.trim().length() == 0) {
if (pools.isBlank()) {
disabled = true;
}

Expand Down Expand Up @@ -101,7 +102,7 @@ public void createRule(Element rule, String creator, String description) {
public void addRule(String id, String pool, Element rule, String description) throws MCRException {
MCRRuleMapping ruleMapping = getAutoGeneratedRuleMapping(rule, "System", pool, id, description);
String oldRuleID = accessStore.getRuleID(id, pool);
if (oldRuleID == null || oldRuleID.equals("")) {
if (oldRuleID == null || oldRuleID.isEmpty()) {
accessStore.createAccessDefinition(ruleMapping);
} else {
accessStore.updateAccessDefinition(ruleMapping);
Expand Down Expand Up @@ -135,7 +136,7 @@ public void removeAllRules(String id) throws MCRException {
public void updateRule(String id, String pool, Element rule, String description) throws MCRException {
MCRRuleMapping ruleMapping = getAutoGeneratedRuleMapping(rule, "System", pool, id, description);
String oldRuleID = accessStore.getRuleID(id, pool);
if (oldRuleID == null || oldRuleID.equals("")) {
if (oldRuleID == null || oldRuleID.isEmpty()) {
LOGGER.debug(
"updateRule called for id <{}> and pool <{}>, but no rule is existing, so new rule was created", id,
pool);
Expand Down Expand Up @@ -347,7 +348,7 @@ public synchronized String getNextFreeRuleID(String prefix) {
*/
@Override
public String getNormalizedRuleString(Element rule) {
if (rule.getChildren() == null || rule.getChildren().size() == 0) {
if (rule.getChildren() == null || rule.getChildren().isEmpty()) {
return "false";
}
Element normalizedRule = normalize(rule.getChildren().getFirst());
Expand All @@ -372,9 +373,9 @@ public MCRRuleMapping getAutoGeneratedRuleMapping(Element rule, String creator,
String description) {
String ruleString = getNormalizedRuleString(rule);
String ruleID = ruleIDTable.get(ruleString);
if (ruleID == null || ruleID.length() == 0) {
if (ruleID == null || ruleID.isEmpty()) {
Collection<String> existingIDs = ruleStore.retrieveRuleIDs(ruleString, description);
if (existingIDs != null && existingIDs.size() > 0) {
if (existingIDs != null && !existingIDs.isEmpty()) {
// rule yet exists
ruleID = existingIDs.iterator().next();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
package org.mycore.access.mcrimpl;

import java.util.HashMap;
import java.util.Map;

/**
* Maps object ids to rules
*
*
* @author Arne Seifert
*/
public class MCRAccessDefinition {

private String objid;

private HashMap<String, String> pools = new HashMap<>();
private Map<String, String> pools = new HashMap<>();

public MCRAccessDefinition() {
pools.clear();
Expand All @@ -43,11 +44,11 @@ public void setObjID(String value) {
objid = value;
}

public HashMap<String, String> getPool() {
public Map<String, String> getPool() {
return pools;
}

public void setPool(HashMap<String, String> pool) {
public void setPool(Map<String, String> pool) {
pools = pool;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.logging.log4j.LogManager;
Expand All @@ -37,7 +38,7 @@
* must implement this interface. Which database actually will be used can then
* be configured by reading the value <code>MCR.Persistence.Access.Store.Class</code>
* from mycore.properties.access
*
*
* @author Arne Seifert
*/
public abstract class MCRAccessStore {
Expand Down Expand Up @@ -71,7 +72,7 @@ public abstract class MCRAccessStore {
public abstract boolean isRuleInUse(String ruleid);

/**
*
*
* @return a collection of all String IDs an access rule is assigned to
*/
public abstract Collection<String> getDistinctStringIDs();
Expand All @@ -96,13 +97,13 @@ public static Collection<String> getPools() {
/**
* alle Elemente eines Datentypes aufbereiten
* @param type document type
*
*
* @return List of MCRAccessDefinition
* @see MCRAccessDefinition
*/
public Collection<MCRAccessDefinition> getDefinition(String type) {
try {
HashMap<String, Collection<String>> sqlDefinition = new HashMap<>();
Map<String, Collection<String>> sqlDefinition = new HashMap<>();
Collection<String> pools = getInstance().getDatabasePools();
//merge pools
pools.removeAll(getPools());
Expand Down
9 changes: 5 additions & 4 deletions mycore-base/src/main/java/org/mycore/common/MCRConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.logging.log4j.LogManager;
import org.jdom2.Namespace;
Expand All @@ -30,7 +31,7 @@
/**
* This class replaces the deprecated MCRDefaults interface and provides some
* final static fields of common use.
*
*
* @author Jens Kupferschmidt
* @author Thomas Scheffler (yagee)
* @author Stefan Freitag (sasf)
Expand Down Expand Up @@ -119,10 +120,10 @@ public final class MCRConstants {

public static final Namespace MCR_NAMESPACE = Namespace.getNamespace("mcr", MCR_URL);

private static final HashMap<String, Namespace> NAMESPACES_BY_PREFIX;
private static final Map<String, Namespace> NAMESPACES_BY_PREFIX;

static {
NAMESPACES_BY_PREFIX = new HashMap<>();
NAMESPACES_BY_PREFIX = new ConcurrentHashMap<>();

Field[] fields = MCRConstants.class.getFields();
for (Field f : fields) {
Expand Down
4 changes: 2 additions & 2 deletions mycore-base/src/main/java/org/mycore/common/MCRMailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected void doGetPost(MCRServletJob job) throws Exception {
public static void send(String sender, String recipient, String subject, String body) {
LOGGER.debug("Called plaintext send method with single recipient.");

ArrayList<String> recipients = new ArrayList<>();
List<String> recipients = new ArrayList<>();
recipients.add(recipient);
send(sender, null, recipients, null, subject, body, null);
}
Expand Down Expand Up @@ -209,7 +209,7 @@ public static void send(String sender, List<String> recipients, String subject,
public static void send(String sender, String recipient, String subject, String body, List<String> parts) {
LOGGER.debug("Called multipart send method with single recipient.");

ArrayList<String> recipients = new ArrayList<>();
List<String> recipients = new ArrayList<>();
recipients.add(recipient);
send(sender, null, recipients, null, subject, body, parts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static Bucket getOrCreateBucket(String configID) {
}
final String dsConfigLimits = MCRConfiguration2.getStringOrThrow(CONFIG_PREFIX + CONFIG_ID + ".Limits");

final HashMap<String, Long> limitMap = Arrays.stream(dsConfigLimits.split(",")).collect(
final Map<String, Long> limitMap = Arrays.stream(dsConfigLimits.split(",")).collect(
HashMap::new, (map, str) -> map.put(str.split("/")[1].trim(),
Long.parseLong(str.split("/")[0].trim())),
HashMap::putAll);
Expand All @@ -96,7 +96,7 @@ public static Bucket getOrCreateBucket(String configID) {
* @param limitMap a map of time units and the corresponding limit/amount of tokens.
* @return the created bucket
*/
private static Bucket createNewBucket(HashMap<String, Long> limitMap) {
private static Bucket createNewBucket(Map<String, Long> limitMap) {
final LocalBucketBuilder builder = Bucket.builder();
for (Map.Entry<String, Long> entry : limitMap.entrySet()) {
final String unit = entry.getKey();
Expand Down
20 changes: 11 additions & 9 deletions mycore-base/src/main/java/org/mycore/common/MCRUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
Expand Down Expand Up @@ -175,8 +177,8 @@ public static Path writeTextToFile(String textToWrite, String fileName, Charset
* the File instance of the basic directory
* @return an ArrayList with file names as pathes
*/
public static ArrayList<String> getAllFileNames(File basedir) {
ArrayList<String> out = new ArrayList<>();
public static List<String> getAllFileNames(File basedir) {
List<String> out = new ArrayList<>();
File[] stage = basedir.listFiles();

for (File element : stage) {
Expand All @@ -199,8 +201,8 @@ public static ArrayList<String> getAllFileNames(File basedir) {
* @param path the part of directory path
* @return an ArrayList with file names as pathes
*/
public static ArrayList<String> getAllFileNames(File basedir, String path) {
ArrayList<String> out = new ArrayList<>();
public static List<String> getAllFileNames(File basedir, String path) {
List<String> out = new ArrayList<>();
File[] stage = basedir.listFiles();

for (File element : stage) {
Expand All @@ -223,8 +225,8 @@ public static ArrayList<String> getAllFileNames(File basedir, String path) {
* the File instance of the basic directory
* @return an ArrayList with directory names as pathes
*/
public static ArrayList<String> getAllDirectoryNames(File basedir) {
ArrayList<String> out = new ArrayList<>();
public static List<String> getAllDirectoryNames(File basedir) {
List<String> out = new ArrayList<>();
File[] stage = basedir.listFiles();

for (File element : stage) {
Expand All @@ -246,8 +248,8 @@ public static ArrayList<String> getAllDirectoryNames(File basedir) {
* the part of directory path
* @return an ArrayList with directory names as pathes
*/
public static ArrayList<String> getAllDirectoryNames(File basedir, String path) {
ArrayList<String> out = new ArrayList<>();
public static List<String> getAllDirectoryNames(File basedir, String path) {
List<String> out = new ArrayList<>();
File[] stage = basedir.listFiles();

for (File element : stage) {
Expand Down Expand Up @@ -409,7 +411,7 @@ public static void untar(Path source, Path expandToDirectory) throws IOException
try (TarArchiveInputStream tain = new TarArchiveInputStream(Files.newInputStream(source))) {
TarArchiveEntry tarEntry;
FileSystem targetFS = expandToDirectory.getFileSystem();
HashMap<Path, FileTime> directoryTimes = new HashMap<>();
Map<Path, FileTime> directoryTimes = new HashMap<>();
while ((tarEntry = tain.getNextEntry()) != null) {
Path target = MCRPathUtils.getPath(targetFS, tarEntry.getName());
Path absoluteTarget = expandToDirectory.resolve(target).normalize().toAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
Expand All @@ -40,7 +41,7 @@
*/
@SuppressWarnings("unchecked")
class MCRConcurrentHashMap<K extends SingletonKey, V> extends ConcurrentHashMap<K, V> {
private HashMap<K, RemappedKey> keyMap = new HashMap<>();
private Map<K, RemappedKey> keyMap = new HashMap<>();

// Disable serialization
@SuppressWarnings("unused")
Expand All @@ -61,7 +62,7 @@ private void writeObject(ObjectOutputStream ois)
* In case of collision, the key is automatically remapped via {@link RemappedKey}.
* It is a wrapper to modify the hashcode of the given SingletonKey to assign a different bucket
* within the internal table, in an attempt to resolve the collision.
*
*
* <p>The mapping function must not modify the map during computation of any key other then a {@link SingletonKey}
* @see java.util.concurrent.ConcurrentHashMap#computeIfAbsent(java.lang.Object, java.util.function.Function)
*/
Expand Down
Loading