Skip to content

Commit

Permalink
Merge pull request #75 from Evolveum/map-iteration
Browse files Browse the repository at this point in the history
Optimize map iterations using entrySet()
  • Loading branch information
semancik committed Apr 9, 2018
2 parents f3c2d1f + 94bb354 commit 2b4660c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 47 deletions.
Expand Up @@ -71,11 +71,11 @@ private void appendOptions(StringBuilder sb) {
}

sb.append('{');
Iterator<String> keys = map.keySet().iterator();
Iterator<Map.Entry<String, String>> keys = map.entrySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
sb.append(key).append(":");
sb.append('\'').append(map.get(key)).append('\'');
final Map.Entry<String, String> key = keys.next();
sb.append(key.getKey()).append(":");
sb.append('\'').append(key.getValue()).append('\'');
if (keys.hasNext()) {
sb.append(",\n");
}
Expand Down
Expand Up @@ -357,18 +357,18 @@ private static List<LocaleDescriptor> loadLocaleDescriptors(Resource resource) t
map.put(key, properties.getProperty(key));
}

for (String key : localeMap.keySet()) {
Map<String, String> localeDefinition = localeMap.get(key);
if (!localeDefinition.containsKey(key + PROP_NAME)
|| !localeDefinition.containsKey(key + PROP_FLAG)) {
for (Map.Entry<String, Map<String, String>> entry : localeMap.entrySet()) {
Map<String, String> localeDefinition = entry.getValue();
if (!localeDefinition.containsKey(entry + PROP_NAME)
|| !localeDefinition.containsKey(entry + PROP_FLAG)) {
continue;
}

LocaleDescriptor descriptor = new LocaleDescriptor(
localeDefinition.get(key + PROP_NAME),
localeDefinition.get(key + PROP_FLAG),
localeDefinition.get(key + PROP_DEFAULT),
WebComponentUtil.getLocaleFromString(key)
localeDefinition.get(entry + PROP_NAME),
localeDefinition.get(entry + PROP_FLAG),
localeDefinition.get(entry + PROP_DEFAULT),
WebComponentUtil.getLocaleFromString(entry.getKey())
);
locales.add(descriptor);
}
Expand Down
Expand Up @@ -543,17 +543,18 @@ public ItemPathHolder transposedPath(List<PathHolderSegment> parentPath) {

private void addExplicitNsDeclarations(StringBuilder sb) {
if (explicitNamespaceDeclarations != null) {
for (String prefix : explicitNamespaceDeclarations.keySet()) {
for (Map.Entry<String, String> prefix : explicitNamespaceDeclarations.entrySet()) {
sb.append("declare ");
final String declaration = explicitNamespaceDeclarations.get(prefix);
if (prefix.equals("")) {
sb.append("default namespace '");
sb.append(explicitNamespaceDeclarations.get(prefix));
sb.append(declaration);
sb.append("'; ");
} else {
sb.append("namespace ");
sb.append(prefix);
sb.append("='");
sb.append(explicitNamespaceDeclarations.get(prefix));
sb.append(declaration);
sb.append("'; ");
}
}
Expand Down
Expand Up @@ -152,13 +152,13 @@ public static QName determineQNameWithNs(QName xsdType){
public static <T> Class<T> getXsdToJavaMapping(QName xsdType) {
Class clazz = xsdToJavaTypeMap.get(xsdType);
if (clazz == null){
Set<QName> keys = xsdToJavaTypeMap.keySet();
for (Iterator<QName> iterator = keys.iterator(); iterator.hasNext();){
QName key = iterator.next();
if (QNameUtil.match(key, xsdType)){
return xsdToJavaTypeMap.get(key);
}
}
Set<Map.Entry<QName, Class>> entries = xsdToJavaTypeMap.entrySet();
for (Map.Entry<QName, Class> entry : entries) {
QName key = entry.getKey();
if (QNameUtil.match(key, xsdType)){
return entry.getValue();
}
}
}
return xsdToJavaTypeMap.get(xsdType);
}
Expand Down
Expand Up @@ -268,10 +268,10 @@ private synchronized void updateOverallStatistics(Map<String, MethodUsageStatist
}

private static void printMap(Map<String, MethodUsageStatistics> logMap, Subsystem subsystem, boolean afterTest){

for(String key: logMap.keySet()){
if(logMap.get(key) != null && subsystem.equals(logMap.get(key).getSubsystem())){
logMap.get(key).appendToLogger(afterTest);
for (Map.Entry<String, MethodUsageStatistics> usage : logMap.entrySet()){
final MethodUsageStatistics value = usage.getValue();
if(subsystem.equals(value.getSubsystem())){
value.appendToLogger(afterTest);
}
}
}
Expand Down
Expand Up @@ -554,9 +554,10 @@ private String generateAttempt(ValuePolicyType policy, int defaultLength, boolea
* first in password
*/
Map<StringLimitType, List<String>> mustBeFirst = new HashMap<>();
for (StringLimitType l : lims.keySet()) {
if (l.isMustBeFirst() != null && l.isMustBeFirst()) {
mustBeFirst.put(l, lims.get(l));
for (Map.Entry<StringLimitType, List<String>> entry : lims.entrySet()) {
final StringLimitType key = entry.getKey();
if (key.isMustBeFirst() != null && key.isMustBeFirst()) {
mustBeFirst.put(key, entry.getValue());
}
}

Expand Down Expand Up @@ -771,26 +772,28 @@ private Map<Integer, List<String>> cardinalityCounter(Map<StringLimitType, List<
List<String> password, Boolean skipMatchedLims, boolean uniquenessReached, OperationResult op) {
HashMap<String, Integer> counter = new HashMap<>();

for (StringLimitType l : lims.keySet()) {
Map<StringLimitType, List<String>> mustBeFirst = new HashMap<>();
for (Map.Entry<StringLimitType, List<String>> entry : lims.entrySet()) {
final StringLimitType key = entry.getKey();
int counterKey = 1;
List<String> chars = lims.get(l);
List<String> chars = entry.getValue();
int i = 0;
if (null != password) {
i = charIntersectionCounter(lims.get(l), password);
i = charIntersectionCounter(entry.getValue(), password);
}
// If max is exceed then error unable to continue
if (l.getMaxOccurs() != null && i > l.getMaxOccurs()) {
OperationResult o = new OperationResult("Limitation check :" + l.getDescription());
if (key.getMaxOccurs() != null && i > key.getMaxOccurs()) {
OperationResult o = new OperationResult("Limitation check :" + key.getDescription());
o.recordFatalError(
"Exceeded maximal value for this limitation. " + i + ">" + l.getMaxOccurs());
"Exceeded maximal value for this limitation. " + i + ">" + key.getMaxOccurs());
op.addSubresult(o);
return null;
// if max is all ready reached or skip enabled for minimal skip
// counting
} else if (l.getMaxOccurs() != null && i == l.getMaxOccurs()) {
} else if (key.getMaxOccurs() != null && i == key.getMaxOccurs()) {
continue;
// other cases minimum is not reached
} else if ((l.getMinOccurs() == null || i >= l.getMinOccurs()) && !skipMatchedLims) {
} else if ((key.getMinOccurs() == null || i >= key.getMinOccurs()) && !skipMatchedLims) {
continue;
}
for (String s : chars) {
Expand All @@ -803,9 +806,9 @@ private Map<Integer, List<String>> cardinalityCounter(Map<StringLimitType, List<
}
}
counterKey++;

}


// If need to remove disabled chars (already reached limitations)
if (null != password) {
for (StringLimitType l : lims.keySet()) {
Expand Down
Expand Up @@ -177,7 +177,7 @@ public void send(Message mailMessage, String transportName, Event event, Task ta
LOGGER.debug("Using mail properties: ");
for (Object key : properties.keySet()) {
if (key instanceof String && ((String) key).startsWith("mail.")) {
LOGGER.debug(" - " + key + " = " + properties.get(key));
LOGGER.debug(" - {} = {}", key, properties.get(key));
}
}
}
Expand Down
Expand Up @@ -1117,9 +1117,9 @@ private void updateFields(Outline outline) {

print("Updating fields and get/set methods: " + classOutline.implClass.fullName());

for (String field : fields.keySet()) {
JFieldVar fieldVar = fields.get(field);
// marks a:rawType fields with @Raw - this has to be executed for any bean, not only for prism containers
for (Map.Entry<String, JFieldVar> field : fields.entrySet()) {
JFieldVar fieldVar = field.getValue();
// marks a:rawType fields with @Raw - this has to be executed for any bean, not only for prism containers
if (hasAnnotation(classOutline, fieldVar, A_RAW_TYPE) != null) {
annotateFieldAsRaw(fieldVar);
}
Expand All @@ -1134,7 +1134,7 @@ private void updateFields(Outline outline) {
}

allFieldsToBeRemoved.forEach((jDefinedClass, jFieldVars) -> {
jFieldVars.forEach(field -> jDefinedClass.removeField(field));
jFieldVars.forEach(jDefinedClass::removeField);
});
}

Expand All @@ -1148,8 +1148,8 @@ private void processContainerFields(ClassOutline classOutline, Map<JDefinedClass
boolean isObject = hasAnnotation(classOutline, A_PRISM_OBJECT);

List<JFieldVar> fieldsToBeRemoved = new ArrayList<>();
for (String field : fields.keySet()) {
JFieldVar fieldVar = fields.get(field);
for (Map.Entry<String, JFieldVar> field : fields.entrySet()) {
JFieldVar fieldVar = field.getValue();
if (isAuxiliaryField(fieldVar)) {
continue;
}
Expand Down Expand Up @@ -1183,8 +1183,8 @@ private void processContainerFields(ClassOutline classOutline, Map<JDefinedClass

private void createFluentFieldMethods(ClassOutline targetClass, ClassOutline sourceClass) {
Map<String, JFieldVar> fields = sourceClass.implClass.fields();
for (String field : fields.keySet()) {
JFieldVar fieldVar = fields.get(field);
for (Map.Entry<String, JFieldVar> field : fields.entrySet()) {
JFieldVar fieldVar = field.getValue();
if (!isAuxiliaryField(fieldVar) && !hasAnnotationClass(fieldVar, XmlAnyElement.class)) {
createFluentFieldMethods(fieldVar, targetClass, sourceClass);
}
Expand Down

0 comments on commit 2b4660c

Please sign in to comment.