123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- /*
- * Copyright (c) 2006-2015 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.events;
-
- import com.dmdirc.interfaces.Connection;
- import com.dmdirc.interfaces.User;
- import com.dmdirc.parser.events.UserInfoEvent;
- import com.dmdirc.parser.events.UserInfoEvent.UserInfoType;
-
- import java.time.LocalDateTime;
- import java.util.Collection;
- import java.util.EnumMap;
- import java.util.Map;
- import java.util.Optional;
-
- /**
- * Event raised when detailed user info has been received for a user.
- */
- public class UserInfoResponseEvent extends ServerDisplayableEvent {
-
- private final User user;
- private final Map<UserInfoType, UserInfoProperty> info;
-
- public UserInfoResponseEvent(final LocalDateTime date, final Connection connection,
- final User user, final Map<UserInfoType, String> info) {
- super(date, connection);
- this.user = user;
- this.info = new EnumMap<>(UserInfoType.class);
- info.forEach((key, value) -> this.info.put(key, new UserInfoProperty(key, value)));
- }
-
- /**
- * Gets the client that the event is for.
- *
- * @return The user this event is for.
- */
- public User getUser() {
- return user;
- }
-
- /**
- * Gets a specific piece of information about the user.
- *
- * @param type The type of information to return.
- *
- * @return An optional containing the information, if it was provided.
- */
- public Optional<String> getInfo(final UserInfoEvent.UserInfoType type) {
- return Optional.ofNullable(info.get(type)).map(UserInfoProperty::getRawValue);
- }
-
- /**
- * Gets a collection of all info properties in the response.
- *
- * @return A collection of all user info properties.
- */
- public Collection<UserInfoProperty> getProperties() {
- return info.values();
- }
-
- public static class UserInfoProperty {
-
- private final UserInfoEvent.UserInfoType type;
- private final String rawValue;
-
- public UserInfoProperty(final UserInfoEvent.UserInfoType type, final String rawValue) {
- this.type = type;
- this.rawValue = rawValue;
- }
-
- public UserInfoEvent.UserInfoType getType() {
- return type;
- }
-
- public String getRawValue() {
- return rawValue;
- }
-
- public String getFriendlyName() {
- return type.name().charAt(0) + type.name().substring(1).toLowerCase().replace('_', ' ');
- }
-
- }
-
- }
|