config-util: use lambdas
[controller.git] / opendaylight / config / config-util / src / main / java / org / opendaylight / controller / config / util / xml / XmlElement.java
index 795c6b62dbd04d252220764feb8d32a24789eaad..f7ecde687ca5212ca3ec185d1fb1eae32042616f 100644 (file)
@@ -9,7 +9,6 @@
 package org.opendaylight.controller.config.util.xml;
 
 import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
 import com.google.common.base.Strings;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
@@ -38,19 +37,19 @@ public final class XmlElement {
     private final Element element;
     private static final Logger LOG = LoggerFactory.getLogger(XmlElement.class);
 
-    private XmlElement(Element element) {
+    private XmlElement(final Element element) {
         this.element = element;
     }
 
-    public static XmlElement fromDomElement(Element e) {
+    public static XmlElement fromDomElement(final Element e) {
         return new XmlElement(e);
     }
 
-    public static XmlElement fromDomDocument(Document xml) {
+    public static XmlElement fromDomDocument(final Document xml) {
         return new XmlElement(xml.getDocumentElement());
     }
 
-    public static XmlElement fromString(String s) throws DocumentedException {
+    public static XmlElement fromString(final String s) throws DocumentedException {
         try {
             return new XmlElement(XmlUtil.readXmlToElement(s));
         } catch (IOException | SAXException e) {
@@ -58,13 +57,13 @@ public final class XmlElement {
         }
     }
 
-    public static XmlElement fromDomElementWithExpected(Element element, String expectedName) throws DocumentedException {
+    public static XmlElement fromDomElementWithExpected(final Element element, final String expectedName) throws DocumentedException {
         XmlElement xmlElement = XmlElement.fromDomElement(element);
         xmlElement.checkName(expectedName);
         return xmlElement;
     }
 
-    public static XmlElement fromDomElementWithExpected(Element element, String expectedName, String expectedNamespace) throws DocumentedException {
+    public static XmlElement fromDomElementWithExpected(final Element element, final String expectedName, final String expectedNamespace) throws DocumentedException {
         XmlElement xmlElement = XmlElement.fromDomElementWithExpected(element, expectedName);
         xmlElement.checkNamespace(expectedNamespace);
         return xmlElement;
@@ -83,9 +82,9 @@ public final class XmlElement {
                 } else {
                     if (!attribKey.startsWith(XmlUtil.XMLNS_ATTRIBUTE_KEY + ":")){
                         throw new DocumentedException("Attribute doesn't start with :",
-                                DocumentedException.ErrorType.application,
-                                DocumentedException.ErrorTag.invalid_value,
-                                DocumentedException.ErrorSeverity.error);
+                                DocumentedException.ErrorType.APPLICATION,
+                                DocumentedException.ErrorTag.INVALID_VALUE,
+                                DocumentedException.ErrorSeverity.ERROR);
                     }
                     prefix = attribKey.substring(XmlUtil.XMLNS_ATTRIBUTE_KEY.length() + 1);
                 }
@@ -104,37 +103,37 @@ public final class XmlElement {
         return namespaces;
     }
 
-    public void checkName(String expectedName) throws UnexpectedElementException {
+    public void checkName(final String expectedName) throws UnexpectedElementException {
         if (!getName().equals(expectedName)){
             throw new UnexpectedElementException(String.format("Expected %s xml element but was %s", expectedName,
                     getName()),
-                    DocumentedException.ErrorType.application,
-                    DocumentedException.ErrorTag.operation_failed,
-                    DocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.APPLICATION,
+                    DocumentedException.ErrorTag.OPERATION_FAILED,
+                    DocumentedException.ErrorSeverity.ERROR);
         }
     }
 
-    public void checkNamespaceAttribute(String expectedNamespace) throws UnexpectedNamespaceException, MissingNameSpaceException {
+    public void checkNamespaceAttribute(final String expectedNamespace) throws UnexpectedNamespaceException, MissingNameSpaceException {
         if (!getNamespaceAttribute().equals(expectedNamespace))
         {
             throw new UnexpectedNamespaceException(String.format("Unexpected namespace %s should be %s",
                     getNamespaceAttribute(),
                     expectedNamespace),
-                    DocumentedException.ErrorType.application,
-                    DocumentedException.ErrorTag.operation_failed,
-                    DocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.APPLICATION,
+                    DocumentedException.ErrorTag.OPERATION_FAILED,
+                    DocumentedException.ErrorSeverity.ERROR);
         }
     }
 
-    public void checkNamespace(String expectedNamespace) throws UnexpectedNamespaceException, MissingNameSpaceException {
+    public void checkNamespace(final String expectedNamespace) throws UnexpectedNamespaceException, MissingNameSpaceException {
         if (!getNamespace().equals(expectedNamespace))
         {
             throw new UnexpectedNamespaceException(String.format("Unexpected namespace %s should be %s",
                     getNamespace(),
                     expectedNamespace),
-                    DocumentedException.ErrorType.application,
-                    DocumentedException.ErrorTag.operation_failed,
-                    DocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.APPLICATION,
+                    DocumentedException.ErrorTag.OPERATION_FAILED,
+                    DocumentedException.ErrorSeverity.ERROR);
         }
     }
 
@@ -146,19 +145,19 @@ public final class XmlElement {
         return element.getTagName();
     }
 
-    public String getAttribute(String attributeName) {
+    public String getAttribute(final String attributeName) {
         return element.getAttribute(attributeName);
     }
 
-    public String getAttribute(String attributeName, String namespace) {
+    public String getAttribute(final String attributeName, final String namespace) {
         return element.getAttributeNS(namespace, attributeName);
     }
 
-    public NodeList getElementsByTagName(String name) {
+    public NodeList getElementsByTagName(final String name) {
         return element.getElementsByTagName(name);
     }
 
-    public void appendChild(Element element) {
+    public void appendChild(final Element element) {
         this.element.appendChild(element);
     }
 
@@ -182,7 +181,7 @@ public final class XmlElement {
     /**
      * Non recursive
      */
-    private List<XmlElement> getChildElementsInternal(ElementFilteringStrategy strat) {
+    private List<XmlElement> getChildElementsInternal(final ElementFilteringStrategy strat) {
         NodeList childNodes = element.getChildNodes();
         final List<XmlElement> result = new ArrayList<>();
         for (int i = 0; i < childNodes.getLength(); i++) {
@@ -199,35 +198,21 @@ public final class XmlElement {
     }
 
     public List<XmlElement> getChildElements() {
-        return getChildElementsInternal(new ElementFilteringStrategy() {
-            @Override
-            public boolean accept(Element e) {
-                return true;
-            }
-        });
+        return getChildElementsInternal(e -> true);
     }
 
-    public List<XmlElement> getChildElementsWithinNamespace(final String childName, String namespace) {
+    public List<XmlElement> getChildElementsWithinNamespace(final String childName, final String namespace) {
         return Lists.newArrayList(Collections2.filter(getChildElementsWithinNamespace(namespace),
-                new Predicate<XmlElement>() {
-                    @Override
-                    public boolean apply(XmlElement xmlElement) {
-                        return xmlElement.getName().equals(childName);
-                    }
-                }));
+                xmlElement -> xmlElement.getName().equals(childName)));
     }
 
     public List<XmlElement> getChildElementsWithinNamespace(final String namespace) {
-        return getChildElementsInternal(new ElementFilteringStrategy() {
-            @Override
-            public boolean accept(Element e) {
-                try {
-                    return XmlElement.fromDomElement(e).getNamespace().equals(namespace);
-                } catch (MissingNameSpaceException e1) {
-                    return false;
-                }
+        return getChildElementsInternal(e -> {
+            try {
+                return XmlElement.fromDomElement(e).getNamespace().equals(namespace);
+            } catch (final MissingNameSpaceException e1) {
+                return false;
             }
-
         });
     }
 
@@ -237,27 +222,24 @@ public final class XmlElement {
      * @return List of child elements
      */
     public List<XmlElement> getChildElements(final String tagName) {
-        return getChildElementsInternal(new ElementFilteringStrategy() {
-            @Override
-            public boolean accept(Element e) {
-                // localName returns pure localName without prefix
-                return e.getLocalName().equals(tagName);
-            }
+        return getChildElementsInternal(e -> {
+            // localName returns pure localName without prefix
+            return e.getLocalName().equals(tagName);
         });
     }
 
-    public XmlElement getOnlyChildElement(String childName) throws DocumentedException {
+    public XmlElement getOnlyChildElement(final String childName) throws DocumentedException {
         List<XmlElement> nameElements = getChildElements(childName);
         if (nameElements.size() != 1){
             throw new DocumentedException("One element " + childName + " expected in " + toString(),
-                    DocumentedException.ErrorType.application,
-                    DocumentedException.ErrorTag.invalid_value,
-                    DocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.APPLICATION,
+                    DocumentedException.ErrorTag.INVALID_VALUE,
+                    DocumentedException.ErrorSeverity.ERROR);
         }
         return nameElements.get(0);
     }
 
-    public Optional<XmlElement> getOnlyChildElementOptionally(String childName) {
+    public Optional<XmlElement> getOnlyChildElementOptionally(final String childName) {
         List<XmlElement> nameElements = getChildElements(childName);
         if (nameElements.size() != 1) {
             return Optional.absent();
@@ -267,19 +249,15 @@ public final class XmlElement {
 
     public Optional<XmlElement> getOnlyChildElementOptionally(final String childName, final String namespace) {
         List<XmlElement> children = getChildElementsWithinNamespace(namespace);
-        children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
-            @Override
-            public boolean apply(XmlElement xmlElement) {
-                return xmlElement.getName().equals(childName);
-            }
-        }));
+        children = Lists.newArrayList(Collections2.filter(children,
+                xmlElement -> xmlElement.getName().equals(childName)));
         if (children.size() != 1){
             return Optional.absent();
         }
         return Optional.of(children.get(0));
     }
 
-    public XmlElement getOnlyChildElementWithSameNamespace(String childName) throws  DocumentedException {
+    public XmlElement getOnlyChildElementWithSameNamespace(final String childName) throws  DocumentedException {
         return getOnlyChildElement(childName, getNamespace());
     }
 
@@ -287,12 +265,8 @@ public final class XmlElement {
         Optional<String> namespace = getNamespaceOptionally();
         if (namespace.isPresent()) {
             List<XmlElement> children = getChildElementsWithinNamespace(namespace.get());
-            children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
-                @Override
-                public boolean apply(XmlElement xmlElement) {
-                    return xmlElement.getName().equals(childName);
-                }
-            }));
+            children = Lists.newArrayList(Collections2.filter(children,
+                    xmlElement -> xmlElement.getName().equals(childName)));
             if (children.size() != 1){
                 return Optional.absent();
             }
@@ -318,20 +292,16 @@ public final class XmlElement {
         return Optional.absent();
     }
 
-    public XmlElement getOnlyChildElement(final String childName, String namespace) throws DocumentedException {
+    public XmlElement getOnlyChildElement(final String childName, final String namespace) throws DocumentedException {
         List<XmlElement> children = getChildElementsWithinNamespace(namespace);
-        children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
-            @Override
-            public boolean apply(XmlElement xmlElement) {
-                return xmlElement.getName().equals(childName);
-            }
-        }));
+        children = Lists.newArrayList(Collections2.filter(children,
+                xmlElement -> xmlElement.getName().equals(childName)));
         if (children.size() != 1){
             throw new DocumentedException(String.format("One element %s:%s expected in %s but was %s", namespace,
                     childName, toString(), children.size()),
-                    DocumentedException.ErrorType.application,
-                    DocumentedException.ErrorTag.invalid_value,
-                    DocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.APPLICATION,
+                    DocumentedException.ErrorTag.INVALID_VALUE,
+                    DocumentedException.ErrorSeverity.ERROR);
         }
 
         return children.get(0);
@@ -342,9 +312,9 @@ public final class XmlElement {
         if (children.size() != 1){
             throw new DocumentedException(String.format( "One element expected in %s but was %s", toString(),
                     children.size()),
-                    DocumentedException.ErrorType.application,
-                    DocumentedException.ErrorTag.invalid_value,
-                    DocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.APPLICATION,
+                    DocumentedException.ErrorTag.INVALID_VALUE,
+                    DocumentedException.ErrorSeverity.ERROR);
         }
         return children.get(0);
     }
@@ -370,9 +340,9 @@ public final class XmlElement {
             }
         }
         throw new DocumentedException(getName() + " should contain text.",
-                DocumentedException.ErrorType.application,
-                DocumentedException.ErrorTag.invalid_value,
-                DocumentedException.ErrorSeverity.error
+                DocumentedException.ErrorType.APPLICATION,
+                DocumentedException.ErrorTag.INVALID_VALUE,
+                DocumentedException.ErrorSeverity.ERROR
         );
     }
 
@@ -392,9 +362,9 @@ public final class XmlElement {
         if (attribute == null || attribute.equals(DEFAULT_NAMESPACE_PREFIX)){
             throw new MissingNameSpaceException(String.format("Element %s must specify namespace",
                     toString()),
-                    DocumentedException.ErrorType.application,
-                    DocumentedException.ErrorTag.operation_failed,
-                    DocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.APPLICATION,
+                    DocumentedException.ErrorTag.OPERATION_FAILED,
+                    DocumentedException.ErrorSeverity.ERROR);
         }
         return attribute;
     }
@@ -420,9 +390,9 @@ public final class XmlElement {
         Optional<String> namespaceURI = getNamespaceOptionally();
         if (!namespaceURI.isPresent()){
             throw new MissingNameSpaceException(String.format("No namespace defined for %s", this),
-                    DocumentedException.ErrorType.application,
-                    DocumentedException.ErrorTag.operation_failed,
-                    DocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.APPLICATION,
+                    DocumentedException.ErrorTag.OPERATION_FAILED,
+                    DocumentedException.ErrorSeverity.ERROR);
         }
         return namespaceURI.get();
     }
@@ -434,7 +404,7 @@ public final class XmlElement {
         if (element.getNamespaceURI() != null) {
             try {
                 sb.append(", namespace='").append(getNamespace()).append('\'');
-            } catch (MissingNameSpaceException e) {
+            } catch (final MissingNameSpaceException e) {
                 LOG.trace("Missing namespace for element.");
             }
         }
@@ -474,16 +444,11 @@ public final class XmlElement {
 
     public List<XmlElement> getChildElementsWithSameNamespace(final String childName) throws MissingNameSpaceException {
         List<XmlElement> children = getChildElementsWithinNamespace(getNamespace());
-        return Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
-            @Override
-            public boolean apply(XmlElement xmlElement) {
-                return xmlElement.getName().equals(childName);
-            }
-        }));
+        return Lists.newArrayList(Collections2.filter(children, xmlElement -> xmlElement.getName().equals(childName)));
     }
 
-    public void checkUnrecognisedElements(List<XmlElement> recognisedElements,
-                                          XmlElement... additionalRecognisedElements) throws DocumentedException {
+    public void checkUnrecognisedElements(final List<XmlElement> recognisedElements,
+                                          final XmlElement... additionalRecognisedElements) throws DocumentedException {
         List<XmlElement> childElements = getChildElements();
         childElements.removeAll(recognisedElements);
         for (XmlElement additionalRecognisedElement : additionalRecognisedElements) {
@@ -491,18 +456,18 @@ public final class XmlElement {
         }
         if (!childElements.isEmpty()){
             throw new DocumentedException(String.format("Unrecognised elements %s in %s", childElements, this),
-                    DocumentedException.ErrorType.application,
-                    DocumentedException.ErrorTag.invalid_value,
-                    DocumentedException.ErrorSeverity.error);
+                    DocumentedException.ErrorType.APPLICATION,
+                    DocumentedException.ErrorTag.INVALID_VALUE,
+                    DocumentedException.ErrorSeverity.ERROR);
         }
     }
 
-    public void checkUnrecognisedElements(XmlElement... additionalRecognisedElements) throws DocumentedException {
+    public void checkUnrecognisedElements(final XmlElement... additionalRecognisedElements) throws DocumentedException {
         checkUnrecognisedElements(Collections.<XmlElement>emptyList(), additionalRecognisedElements);
     }
 
     @Override
-    public boolean equals(Object o) {
+    public boolean equals(final Object o) {
         if (this == o) {
             return true;
         }
@@ -522,12 +487,7 @@ public final class XmlElement {
     }
 
     public boolean hasNamespace() {
-        if (!getNamespaceAttributeOptionally().isPresent()) {
-            if (!getNamespaceOptionally().isPresent()) {
-                return false;
-            }
-        }
-        return true;
+        return getNamespaceAttributeOptionally().isPresent() || getNamespaceOptionally().isPresent();
     }
 
     private interface ElementFilteringStrategy {