MessageView.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software, 2019-2021 Florian Schmaus
  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.smack.packet;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Set;

  23. import org.jivesoftware.smack.packet.Message.Subject;
  24. import org.jivesoftware.smack.util.Objects;

  25. public interface MessageView extends StanzaView {

  26.     /**
  27.      * Returns the type of the message. If no type has been set this method will return {@link
  28.      * org.jivesoftware.smack.packet.Message.Type#normal}.
  29.      *
  30.      * @return the type of the message.
  31.      */
  32.     Message.Type getType();

  33.     /**
  34.      * Returns the default subject of the message, or null if the subject has not been set.
  35.      * The subject is a short description of message contents.
  36.      * <p>
  37.      * The default subject of a message is the subject that corresponds to the message's language.
  38.      * (see {@link #getLanguage()}) or if no language is set to the applications default
  39.      * language (see {@link Stanza#getDefaultLanguage()}).
  40.      *
  41.      * @return the subject of the message.
  42.      */
  43.     default String getSubject() {
  44.         return getSubject(null);
  45.     }

  46.     /**
  47.      * Returns the subject corresponding to the language. If the language is null, the method result
  48.      * will be the same as {@link #getSubject()}. Null will be returned if the language does not have
  49.      * a corresponding subject.
  50.      *
  51.      * @param language the language of the subject to return.
  52.      * @return the subject related to the passed in language.
  53.      */
  54.     default String getSubject(String language) {
  55.         Subject subject = getMessageSubject(language);
  56.         return subject == null ? null : subject.getSubject();
  57.     }

  58.     default Message.Subject getMessageSubject(String language) {
  59.         language = Stanza.determineLanguage(this, language);
  60.         for (Message.Subject subject : getSubjects()) {
  61.             if (Objects.equals(language, subject.getLanguage())
  62.                             || (subject.getLanguage() == null && Objects.equals(getLanguage(), language))) {
  63.                 return subject;
  64.             }
  65.         }
  66.         return null;
  67.     }

  68.     /**
  69.      * Returns a set of all subjects in this Message, including the default message subject accessible
  70.      * from {@link #getSubject()}.
  71.      *
  72.      * @return a collection of all subjects in this message.
  73.      */
  74.     default Set<Message.Subject> getSubjects() {
  75.         List<Message.Subject> subjectList = getExtensions(Subject.class);

  76.         Set<Message.Subject> subjects = new HashSet<>(subjectList.size());
  77.         subjects.addAll(subjectList);

  78.         return subjects;
  79.     }

  80.     /**
  81.      * Returns all the languages being used for the subjects, not including the default subject.
  82.      *
  83.      * @return the languages being used for the subjects.
  84.      */
  85.     default List<String> getSubjectLanguages() {
  86.         Message.Subject defaultSubject = getMessageSubject(null);
  87.         List<String> languages = new ArrayList<String>();
  88.         for (Message.Subject subject : getExtensions(Message.Subject.class)) {
  89.             if (!subject.equals(defaultSubject)) {
  90.                 languages.add(subject.getLanguage());
  91.             }
  92.         }
  93.         return Collections.unmodifiableList(languages);
  94.     }

  95.     /**
  96.      * Returns the default body of the message, or null if the body has not been set. The body
  97.      * is the main message contents.
  98.      * <p>
  99.      * The default body of a message is the body that corresponds to the message's language.
  100.      * (see {@link #getLanguage()}) or if no language is set to the applications default
  101.      * language (see {@link Stanza#getDefaultLanguage()}).
  102.      *
  103.      * @return the body of the message.
  104.      */
  105.     default String getBody() {
  106.         return getBody(getLanguage());
  107.     }

  108.     /**
  109.      * Returns the body corresponding to the language. If the language is null, the method result
  110.      * will be the same as {@link #getBody()}. Null will be returned if the language does not have
  111.      * a corresponding body.
  112.      *
  113.      * @param language the language of the body to return.
  114.      * @return the body related to the passed in language.
  115.      * @since 3.0.2
  116.      */
  117.     default String getBody(String language) {
  118.         Message.Body body = getMessageBody(language);
  119.         return body == null ? null : body.getMessage();
  120.     }

  121.     default Message.Body getMessageBody(String language) {
  122.         language = Stanza.determineLanguage(this, language);
  123.         for (Message.Body body : getBodies()) {
  124.             if (Objects.equals(language, body.getLanguage()) || (language != null && language.equals(getLanguage()) && body.getLanguage() == null)) {
  125.                 return body;
  126.             }
  127.         }
  128.         return null;
  129.     }

  130.     /**
  131.      * Returns a set of all bodies in this Message, including the default message body accessible
  132.      * from {@link #getBody()}.
  133.      *
  134.      * @return a collection of all bodies in this Message.
  135.      * @since 3.0.2
  136.      */
  137.     default Set<Message.Body> getBodies() {
  138.         List<XmlElement> bodiesList = getExtensions(Message.Body.QNAME);
  139.         Set<Message.Body> resultSet = new HashSet<>(bodiesList.size());
  140.         for (XmlElement extensionElement : bodiesList) {
  141.             Message.Body body = (Message.Body) extensionElement;
  142.             resultSet.add(body);
  143.         }
  144.         return resultSet;
  145.     }

  146.     /**
  147.      * Returns all the languages being used for the bodies, not including the default body.
  148.      *
  149.      * @return the languages being used for the bodies.
  150.      * @since 3.0.2
  151.      */
  152.     default List<String> getBodyLanguages() {
  153.         Message.Body defaultBody = getMessageBody(null);
  154.         List<String> languages = new ArrayList<String>();
  155.         for (Message.Body body : getBodies()) {
  156.             if (!body.equals(defaultBody)) {
  157.                 languages.add(body.getLanguage());
  158.             }
  159.         }
  160.         return Collections.unmodifiableList(languages);
  161.     }

  162.     /**
  163.      * Returns the thread id of the message, which is a unique identifier for a sequence
  164.      * of "chat" messages. If no thread id is set, <code>null</code> will be returned.
  165.      *
  166.      * @return the thread id of the message, or <code>null</code> if it doesn't exist.
  167.      */
  168.     default String getThread() {
  169.         Message.Thread thread = getExtension(Message.Thread.class);
  170.         if (thread == null) {
  171.             return null;
  172.         }
  173.         return thread.getThread();
  174.     }
  175. }