This repository has been archived on 2022-12-21. You can view files and clone it, but cannot push or open issues or pull requests.
sdm03/src/main/java/com/github/dtschust/zork/parser/Property.java

92 lines
2.8 KiB
Java

package com.github.dtschust.zork.parser;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* A property is an encoding-agnostic representation of a 3-tuple made of:
* - A property name (string);
* - A property value (string);
* - An (optional) list of sub-properties.
*/
public interface Property {
/**
* Returns the property name
*
* @return the property name
*/
String name();
/**
* Returns the property value
*
* @return the property value
*/
String value();
/**
* Returns a list of all sub-properties
*
* @return a list af all sub-properties
*/
List<? extends Property> subProperties();
/**
* Returns a list of the sub-properties matching the given name
*
* @param name the name of the sub-properties
* @return a list of sub-properties
*/
List<? extends Property> subPropertiesByName(String name);
/**
* Given a sub-property name and an optional default value, returns:
* - The sub-property value for the first sub-property with the given name, or;
* - If no such sub-properties are found and if the default value is given, the default value, or;
* - nothing, throwing an exception, if no default value is given
*
* @param elementName the sub-property name
* @param defaultValue the default value or Optional.empty()
* @return the sub-property value
* @throws IllegalStateException when the default value is not given and no sub-property is found
*/
String subPropertyValue(String elementName, Optional<String> defaultValue);
/**
* Returns whether at least one sub-property with the given name is found
*
* @param name the sub-property name
* @return whether at least one sub-property with the given name is found
*/
boolean hasSubProperty(String name);
/**
* Overload of {@link #subPropertyValue(String, Optional)} with empty default value
*/
default String subPropertyValue(String name) {
return subPropertyValue(name, Optional.empty());
}
/**
* Overload of {@link #subPropertyValue(String, Optional)} with the given default value
* boxed as an {@link Optional}
*/
default String subPropertyValue(String name, String defaultValue) {
return subPropertyValue(name, Optional.of(defaultValue));
}
/**
* Returns a list of property values for the sub-properties matching the given name
*
* @param propertyName the name of the sub-properties
* @return a list of sub-property values
*/
default List<String> subPropertyValues(final String propertyName) {
return subPropertiesByName(propertyName).stream()
.map(Property::value)
.collect(Collectors.toList());
}
}