All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.codecentric.boot.admin.server.domain.values.Endpoints Maven / Gradle / Ivy

/*
 * Copyright 2014-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.codecentric.boot.admin.server.domain.values;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

import javax.annotation.Nullable;

import static java.util.stream.Collectors.toMap;

@lombok.EqualsAndHashCode
@lombok.ToString
public final class Endpoints implements Iterable, Serializable {

	private final Map endpoints;

	private static final Endpoints EMPTY = new Endpoints(Collections.emptyList());

	private Endpoints(Collection endpoints) {
		if (endpoints.isEmpty()) {
			this.endpoints = Collections.emptyMap();
		}
		else {
			this.endpoints = endpoints.stream().collect(toMap(Endpoint::getId, Function.identity()));
		}
	}

	public Optional get(String id) {
		return Optional.ofNullable(this.endpoints.get(id));
	}

	public boolean isPresent(String id) {
		return this.endpoints.containsKey(id);
	}

	@Override
	public Iterator iterator() {
		return new UnmodifiableIterator<>(this.endpoints.values().iterator());
	}

	public static Endpoints empty() {
		return EMPTY;
	}

	public static Endpoints single(String id, String url) {
		return new Endpoints(Collections.singletonList(Endpoint.of(id, url)));
	}

	public static Endpoints of(@Nullable Collection endpoints) {
		if (endpoints == null || endpoints.isEmpty()) {
			return empty();
		}
		return new Endpoints(endpoints);
	}

	public Endpoints withEndpoint(String id, String url) {
		Endpoint endpoint = Endpoint.of(id, url);
		HashMap newEndpoints = new HashMap<>(this.endpoints);
		newEndpoints.put(endpoint.getId(), endpoint);
		return new Endpoints(newEndpoints.values());
	}

	public Stream stream() {
		return this.endpoints.values().stream();
	}

	private static final class UnmodifiableIterator implements Iterator {

		private final Iterator delegate;

		private UnmodifiableIterator(Iterator delegate) {
			this.delegate = delegate;
		}

		@Override
		public boolean hasNext() {
			return this.delegate.hasNext();
		}

		@Override
		public T next() {
			return this.delegate.next();
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy