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

org.johnnei.enjin.spec.dto.User Maven / Gradle / Ivy

The newest version!
package org.johnnei.enjin.spec.dto;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

public class User {

	private String username;

	private String session_id;

	private String user_id;

	private Map notification_counts;

	private Map pusher_channels;

	private String resources;

	private List sites;

	User() {
		// Constructor for serialization
	}

	public User(String sessionId, CheckSessionResult session) {
		username = session.getUsername();
		this.session_id = sessionId;
		user_id = session.getUserId();
		notification_counts = Collections.unmodifiableMap(session.getNotificationCounts());
		pusher_channels = Collections.unmodifiableMap(session.getPusherChannels());
		resources = session.getResources();
		sites = Collections.unmodifiableList(session.getSites());
	}

	private User(Builder builder) {
		username = builder.username;
		session_id = builder.session_id;
		user_id = builder.user_id;
		notification_counts = Collections.unmodifiableMap(builder.notification_counts);
		pusher_channels = Collections.unmodifiableMap(builder.pusher_channels);
		resources = builder.resources;
		sites = Collections.unmodifiableList(builder.sites);
	}

	public String getUsername() {
		return username;
	}

	public String getSessionId() {
		return session_id;
	}

	public String getUserId() {
		return user_id;
	}

	/**
	 * Gets the amount of notification in the given type/category.
	 * @param type
	 *        The type/category of the notification
	 * @return
	 *        The amount of notifications (or 0 if the type doesn't exist)
	 */
	public Optional getNotificationCount(String type) {
		if (notification_counts.containsKey(type)) {
			return Optional.of(notification_counts.get(type));
		}

		return Optional.empty();
	}

	public Stream> getNotificationCounts() {
		return notification_counts.entrySet().stream();
	}

	public Optional getPushChannelValue(String key) {
		if (pusher_channels.containsKey(key)) {
			return Optional.of(pusher_channels.get(key));
		}

		return Optional.empty();
	}

	public Stream> getPusherChannels() {
		return pusher_channels.entrySet().stream();
	}

	public String getResources() {
		return resources;
	}

	public Stream getSites() {
		return sites.stream();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((resources == null) ? 0 : resources.hashCode());
		result = prime * result + ((user_id == null) ? 0 : user_id.hashCode());
		result = prime * result + ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof User)) {
			return false;
		}

		User other = (User) obj;
		if (!Objects.equals(resources, other.resources)) {
			return false;
		}

		if (!Objects.equals(user_id, other.user_id)) {
			return false;
		}

		if (!Objects.equals(username, other.username)) {
			return false;
		}

		return true;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("User [user_id=");
		builder.append(user_id);
		builder.append(", username=");
		builder.append(username);
		builder.append(", notifications=");
		builder.append(notification_counts.values().stream().mapToInt(n -> n).sum());
		builder.append(", pusher_channels= [");
		builder.append(pusher_channels.keySet().stream().reduce("", (a, b) -> a + ", " + b));
		builder.append("] , resources=");
		builder.append(resources);
		builder.append(", sites=[ ");
		builder.append(sites.stream().map(s -> s.getName()).reduce("", (a, b) -> a + b));
		builder.append("]]");
		return builder.toString();
	}

	public static class Builder {

		private String username;

		private String session_id;

		private String user_id;

		private Map notification_counts;

		private Map pusher_channels;

		private String resources;

		private List sites;

		public Builder() {
			notification_counts = new HashMap<>();
			pusher_channels = new HashMap<>();
			sites = new ArrayList<>();
		}

		public Builder setUsername(String username) {
			this.username = username;
			return this;
		}

		public Builder setSessionId(String session_id) {
			this.session_id = session_id;
			return this;
		}

		public Builder setUserId(String user_id) {
			this.user_id = user_id;
			return this;
		}

		public Builder addNotificationCount(String type, int count) {
			notification_counts.put(type, count);
			return this;
		}

		public Builder addPusherChannels(String name, String value) {
			pusher_channels.put(name, value);
			return this;
		}

		public Builder setResources(String resources) {
			this.resources = resources;
			return this;
		}

		public Builder addSite(Site site) {
			sites.add(site);
			return this;
		}

		public User build() {
			return new User(this);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy