public Object apply()

in spring-ai-alibaba-graph/spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/state/strategy/AppendStrategy.java [34:92]


	public Object apply(Object oldValue, Object newValue) {
		if (newValue == null) {
			return oldValue;
		}

		if (oldValue instanceof Optional<?> oldValueOptional) {
			oldValue = oldValueOptional.orElse(null);
		}

		boolean oldValueIsList = oldValue instanceof List<?>;

		if (oldValueIsList && newValue instanceof AppenderChannel.RemoveIdentifier<?>) {
			var result = new ArrayList<>((List<Object>) oldValue);
			removeFromList(result, (AppenderChannel.RemoveIdentifier) newValue);
			return unmodifiableList(result);
		}

		List<Object> list = null;
		if (newValue instanceof List) {
			list = new ArrayList<>((List<?>) newValue);
		}
		else if (newValue.getClass().isArray()) {
			list = Arrays.asList((Object[]) newValue);
		}
		else if (newValue instanceof Collection) {
			list = new ArrayList<>((Collection<?>) newValue);
		}

		if (oldValueIsList) {
			List<Object> oldList = (List<Object>) oldValue;
			if (list != null) {
				if (list.isEmpty()) {
					return oldValue;
				}
				if (oldValueIsList) {
					var result = evaluateRemoval((List<Object>) oldValue, list);
					List<Object> mergedList = Stream.concat(result.oldValues().stream(), result.newValues().stream())
						.distinct()
						.collect(Collectors.toList());
					return mergedList;
				}
				oldList.addAll(list);
			}
			else {
				oldList.add(newValue);
			}
			return oldList;
		}
		else {
			ArrayList<Object> arrayResult = new ArrayList<>();
			if (list != null) {
				arrayResult.addAll(list);
			}
			else {
				arrayResult.add(newValue);
			}
			return arrayResult;
		}
	}