Skip to content

Instantly share code, notes, and snippets.

@teocci
Created January 26, 2024 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teocci/0297494230d8c0c2a557a5af5d93dcea to your computer and use it in GitHub Desktop.
Save teocci/0297494230d8c0c2a557a5af5d93dcea to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.LongStream;
public class ObjectUpdateExample {
public static <T> void printArray(List<T> list) {
printArray(list.toArray());
}
public static <T> void printArray(T[] array) {
if (array == null || array.length < 1) {
System.out.println("[]");
return;
}
T lastItem = array[array.length - 1];
System.out.println("[");
for (T item : array) {
String tail = lastItem == item ? "" : ",";
System.out.printf(" %s%s\n", item, tail);
}
System.out.println("]");
}
public static void main(String[] args) {
TargetDTO dto = new TargetDTO();
System.out.printf("Before update: %s\n", dto);
updateObject(dto);
System.out.printf("After update: %s\n", dto);
List<SelectionDefinition> definitions = new ArrayList<>();
LongStream.range(1, 10).forEach(val -> {
SelectionDefinition definition = new SelectionDefinition();
definition.id = val;
definition.label = String.format("Definition[%d]", val);
definitions.add(definition);
});
printArray(definitions);
}
static void updateObject(TargetDTO dto) {
dto.selection = new SelectionDefinition();
String label = dto.taxonomy.label;
dto.taxonomy.label = label.trim();
}
}
class TargetDTO {
public Long id;
public String location;
public TaxonomyDTO taxonomy;
public String status;
public SelectionDefinition selection;
public TargetDTO() {
id = 10L;
location = "home";
taxonomy = new TaxonomyDTO();
status = "created";
}
@Override
public String toString() {
return '{' +
"id:" + id +
", location:'" + location + '\'' +
", taxonomy:'" + taxonomy + '\'' +
", status:'" + status + '\'' +
", selection:'" + selection + '\'' +
'}';
}
}
class TaxonomyDTO {
public Long id;
public String label;
public TaxonomyDTO() {
id = 8L;
label = "station test 003";
}
@Override
public String toString() {
return '{' +
"id:" + id +
", label:'" + label + '\'' +
'}';
}
}
class SelectionDefinition {
public Long id;
public String label;
public SelectionDefinition() {
id = 1L;
label = "okay";
}
@Override
public String toString() {
return '{' +
"id:" + id +
", label:'" + label + '\'' +
'}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment