
org.opentcs.access.to.CreationTO Maven / Gradle / Ivy
/**
* Copyright (c) The openTCS Authors.
*
* This program is free software and subject to the MIT license. (For details,
* see the licensing information (LICENSE.txt) you should have received with
* this copy of the software.)
*/
package org.opentcs.access.to;
import static java.util.Objects.requireNonNull;
import jakarta.annotation.Nonnull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* The base class for all creation transfer objects.
*/
public class CreationTO
implements
Serializable {
/**
* The name of this transfer object.
*/
@Nonnull
private final String name;
/**
* The properties of this transfer object.
*/
@Nonnull
private final Map properties;
/**
* Creates a new instance.
*
* @param name The name of this transfer object.
*/
public CreationTO(
@Nonnull
String name
) {
this.name = requireNonNull(name, "name");
this.properties = Map.of();
}
protected CreationTO(
@Nonnull
String name,
@Nonnull
Map properties
) {
this.name = requireNonNull(name, "name");
this.properties = requireNonNull(properties, "properties");
}
/**
* Returns the name of this transfer object.
*
* @return The name of this transfer object.
*/
@Nonnull
public String getName() {
return name;
}
/**
* Creates a copy of this object with the given name.
*
* @param name the new name
* @return A copy of this object, differing in the given value.
*/
public CreationTO withName(
@Nonnull
String name
) {
return new CreationTO(
name,
properties
);
}
/**
* Returns the properties of this transfer object in an unmodifiable map.
*
* @return The properties of this transfer object in an unmodifiable map.
*/
@Nonnull
public Map getProperties() {
return Collections.unmodifiableMap(properties);
}
/**
* Returns the properties of this transfer object.
*
* @return The properties of this transfer object.
*/
protected Map getModifiableProperties() {
return properties;
}
/**
* Creates a copy of this object with the given properties.
*
* @param properties The properties.
* @return A copy of this object with the given properties.
*/
public CreationTO withProperties(
@Nonnull
Map properties
) {
return new CreationTO(name, properties);
}
/**
* Creates a copy of this object with the given property.
* If value == null is true then the key-value pair is removed from the properties.
*
* @param key the key.
* @param value the value
* @return A copy of this object that includes the given property or
* removes the entry, if value == null.
*/
public CreationTO withProperty(
@Nonnull
String key,
@Nonnull
String value
) {
return new CreationTO(
name,
propertiesWith(key, value)
);
}
protected final Map propertiesWith(String key, String value) {
return mapWithMapping(properties, key, value);
}
/**
* Returns a new map, with the mappings of the given map and the given mapping added to it.
*
* @param The type of the map's keys.
* @param The type of the map's values.
* @param map The map to be extended.
* @param key The key.
* @param value The value. May be null
to remove the mapping from the given map.
* @return a new map, with the mappings of the given map and the given mapping added to it.
*/
protected static final Map mapWithMapping(Map map, K key, V value) {
requireNonNull(map, "map");
requireNonNull(key, "key");
Map result = new HashMap<>(map);
if (value == null) {
result.remove(key);
}
else {
result.put(key, value);
}
return result;
}
/**
* Returns a new list, with the elements of the given list and the given element added to it.
*
* @param The element type of the list.
* @param list The list to be extended.
* @param newElement The element to be added to the list.
* @return A new list, consisting of the given list and the given element added to it.
*/
protected static final List listWithAppendix(List list, T newElement) {
List result = new ArrayList<>(list.size() + 1);
result.addAll(list);
result.add(newElement);
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy