SpoilerManager.java

  1. /**
  2.  *
  3.  * Copyright 2018 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.spoiler;

  18. import java.util.Map;
  19. import java.util.WeakHashMap;

  20. import org.jivesoftware.smack.Manager;
  21. import org.jivesoftware.smack.XMPPConnection;

  22. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;

  23. public final class SpoilerManager extends Manager {

  24.     public static final String NAMESPACE_0 = "urn:xmpp:spoiler:0";

  25.     private static final Map<XMPPConnection, SpoilerManager> INSTANCES = new WeakHashMap<>();

  26.     private final ServiceDiscoveryManager serviceDiscoveryManager;

  27.     /**
  28.      * Create a new SpoilerManager and add Spoiler to disco features.
  29.      *
  30.      * @param connection xmpp connection
  31.      */
  32.     private SpoilerManager(XMPPConnection connection) {
  33.         super(connection);
  34.         serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
  35.     }

  36.     /**
  37.      * Begin announcing support for Spoiler messages.
  38.      */
  39.     public void startAnnounceSupport() {
  40.         serviceDiscoveryManager.addFeature(NAMESPACE_0);
  41.     }

  42.     /**
  43.      * End announcing support for Spoiler messages.
  44.      */
  45.     public void stopAnnounceSupport() {
  46.         serviceDiscoveryManager.removeFeature(NAMESPACE_0);
  47.     }

  48.     /**
  49.      * Return the connections instance of the SpoilerManager.
  50.      *
  51.      * @param connection xmpp connection
  52.      * @return SpoilerManager TODO javadoc me please
  53.      */
  54.     public static synchronized SpoilerManager getInstanceFor(XMPPConnection connection) {
  55.         SpoilerManager manager = INSTANCES.get(connection);
  56.         if (manager == null) {
  57.             manager = new SpoilerManager(connection);
  58.             INSTANCES.put(connection, manager);
  59.         }
  60.         return manager;
  61.     }
  62. }