Home Reference Source

src/groups/Group.js

import {Set} from 'immutable';

/**
 * This class allows access to the details of each group
 * @link {GroupManager#.getGroups()} method should be used to obtain the list
 * of instances of this class.
 */
export class Group {
	/** @protected */
	static of({id, name, participants, phonebook} = {}) {
		return new Group(id, name, participants, phonebook);
	}

	/** @private */
	constructor(id, name, participants, phonebook) {
		/** @private */
		this.id = id;
		/** @private */
		this.name = name;
		/** @private */
		this.participants = new Set(participants);
		/** @private */
		this.phonebook = new Set(phonebook);

		Object.freeze(this);
	}

	/**
	 * The unique id that identifies the group
	 * @return {string}
	 */
	getId() {
		return this.id;
	}

	/**
	 * Get the name of a group
	 * @return {string}
	 */
	getName() {
		return this.name;
	}

	/**
	 * Get a list of id of users that belong to the group
	 * @return {ImmutableSet<string>}
	 */
	getParticipants() {
		return this.participants;
	}

	/**
	 * Returns the phonebook associated to this group
	 * @return {ImmutableSet<Object>}
	 * @property {string} name
	 * @property {string} address
	 */
	getPhonebook() {
		return this.phonebook;
	}

	/**
	 * Creates a new Group object with the provided parameter changed
	 * @param {Object} args
	 * @param {string} [args.name]
	 * @param {Iterable<string>} [args.participants]
	 * @return {Group}
	 */
	with({name = this.name, participants = this.participants} = {}) {
		return new Group(this.id, name, participants);
	}

	/**
	 * Creates a new Group object adding the provided participants
	 * @param {...string} participants
	 * @return {Group}
	 */
	plusParticipants(...participants) {
		return this.with({participants: this.participants.union(participants)});
	}

	/**
	 * Creates a new Group object removing the provided participants
	 * @param {...string} participants
	 * @return {Group}
	 */
	minusParticipants(...participants) {
		return this.with({participants: this.participants.subtract(participants)});
	}
}