You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserInfoResponseEvent.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.events;
  23. import com.dmdirc.interfaces.Connection;
  24. import com.dmdirc.interfaces.User;
  25. import com.dmdirc.parser.events.UserInfoEvent;
  26. import com.dmdirc.parser.events.UserInfoEvent.UserInfoType;
  27. import java.time.LocalDateTime;
  28. import java.util.Collection;
  29. import java.util.EnumMap;
  30. import java.util.Map;
  31. import java.util.Optional;
  32. /**
  33. * Event raised when detailed user info has been received for a user.
  34. */
  35. public class UserInfoResponseEvent extends ServerDisplayableEvent {
  36. private final User user;
  37. private final Map<UserInfoType, UserInfoProperty> info;
  38. public UserInfoResponseEvent(final LocalDateTime date, final Connection connection,
  39. final User user, final Map<UserInfoType, String> info) {
  40. super(date, connection);
  41. this.user = user;
  42. this.info = new EnumMap<>(UserInfoType.class);
  43. info.forEach((key, value) -> this.info.put(key, new UserInfoProperty(key, value)));
  44. }
  45. /**
  46. * Gets the client that the event is for.
  47. *
  48. * @return The user this event is for.
  49. */
  50. public User getUser() {
  51. return user;
  52. }
  53. /**
  54. * Gets a specific piece of information about the user.
  55. *
  56. * @param type The type of information to return.
  57. *
  58. * @return An optional containing the information, if it was provided.
  59. */
  60. public Optional<String> getInfo(final UserInfoEvent.UserInfoType type) {
  61. return Optional.ofNullable(info.get(type)).map(UserInfoProperty::getRawValue);
  62. }
  63. /**
  64. * Gets a collection of all info properties in the response.
  65. *
  66. * @return A collection of all user info properties.
  67. */
  68. public Collection<UserInfoProperty> getProperties() {
  69. return info.values();
  70. }
  71. public static class UserInfoProperty {
  72. private final UserInfoEvent.UserInfoType type;
  73. private final String rawValue;
  74. public UserInfoProperty(final UserInfoEvent.UserInfoType type, final String rawValue) {
  75. this.type = type;
  76. this.rawValue = rawValue;
  77. }
  78. public UserInfoEvent.UserInfoType getType() {
  79. return type;
  80. }
  81. public String getRawValue() {
  82. return rawValue;
  83. }
  84. public String getFriendlyName() {
  85. return type.name().charAt(0) + type.name().substring(1).toLowerCase().replace('_', ' ');
  86. }
  87. }
  88. }