BasicRosterItem.java 4.53 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16
package org.jivesoftware.messenger.user;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Derek DeMoro's avatar
Derek DeMoro committed
17
import org.xmpp.packet.JID;
Matt Tucker's avatar
Matt Tucker committed
18 19 20 21 22 23 24 25 26

/**
 * <p>Implements the basic RosterItem interface storing all data into simple fields.</p>
 * <p>This class is intended to be used as a simple based for creating specialized RosterItem
 * implementations without having to recode the very boring and copies set/get accessor methods.</p>
 *
 * @author Iain Shigeoka
 */
public class BasicRosterItem implements RosterItem {
Derek DeMoro's avatar
Derek DeMoro committed
27 28 29
    protected RecvType recvStatus;
    protected JID jid;
    protected String nickname;
Gaston Dombiak's avatar
Gaston Dombiak committed
30
    protected List<String> groups;
Derek DeMoro's avatar
Derek DeMoro committed
31 32 33 34 35
    protected SubType subStatus;
    protected AskType askStatus;


    public BasicRosterItem(JID jid,
Matt Tucker's avatar
Matt Tucker committed
36 37 38 39
                           SubType subStatus,
                           AskType askStatus,
                           RecvType recvStatus,
                           String nickname,
Gaston Dombiak's avatar
Gaston Dombiak committed
40
                           List<String> groups) {
Matt Tucker's avatar
Matt Tucker committed
41 42 43 44 45
        this.jid = jid;
        this.subStatus = subStatus;
        this.askStatus = askStatus;
        this.recvStatus = recvStatus;
        this.nickname = nickname;
Gaston Dombiak's avatar
Gaston Dombiak committed
46
        this.groups = new LinkedList<String>();
Matt Tucker's avatar
Matt Tucker committed
47
        if (groups != null) {
Gaston Dombiak's avatar
Gaston Dombiak committed
48
            Iterator<String> groupItr = groups.iterator();
Matt Tucker's avatar
Matt Tucker committed
49 50 51 52 53 54
            while (groupItr.hasNext()) {
                this.groups.add(groupItr.next());
            }
        }
    }

Derek DeMoro's avatar
Derek DeMoro committed
55
    public BasicRosterItem(JID jid) {
Matt Tucker's avatar
Matt Tucker committed
56 57 58 59 60 61 62 63
        this(jid,
                RosterItem.SUB_NONE,
                RosterItem.ASK_NONE,
                RosterItem.RECV_NONE,
                null,
                null);
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
64
    public BasicRosterItem(JID jid, String nickname, List<String> groups) {
Matt Tucker's avatar
Matt Tucker committed
65 66 67 68 69 70 71 72 73 74 75 76 77
        this(jid,
                RosterItem.SUB_NONE,
                RosterItem.ASK_NONE,
                RosterItem.RECV_NONE,
                nickname,
                groups);
    }

    /**
     * <p>Create a roster item from the data in another one.</p>
     *
     * @param item
     */
Gaston Dombiak's avatar
Gaston Dombiak committed
78 79 80 81 82 83 84
    public BasicRosterItem(org.xmpp.packet.Roster.Item item) {
        this(item.getJID(),
                getSubType(item),
                getAskStatus(item),
                RosterItem.RECV_NONE,
                item.getName(),
                new LinkedList<String>(item.getGroups()));
Matt Tucker's avatar
Matt Tucker committed
85 86
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
87 88 89 90 91 92 93 94 95 96 97
    private static RosterItem.AskType getAskStatus(org.xmpp.packet.Roster.Item item) {
        if (item.getAsk() == org.xmpp.packet.Roster.Ask.subscribe) {
            return RosterItem.ASK_SUBSCRIBE;
        }
        else if (item.getAsk() == org.xmpp.packet.Roster.Ask.unsubscribe) {
            return RosterItem.ASK_UNSUBSCRIBE;
        }
        else {
            return RosterItem.ASK_NONE;
        }
    }
Derek DeMoro's avatar
Derek DeMoro committed
98

Gaston Dombiak's avatar
Gaston Dombiak committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    private static RosterItem.SubType getSubType(org.xmpp.packet.Roster.Item item) {
        if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.to) {
            return RosterItem.SUB_TO;
        }
        else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.from) {
            return RosterItem.SUB_FROM;
        }
        else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.both) {
            return RosterItem.SUB_BOTH;
        }
        else if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
            return RosterItem.SUB_REMOVE;
        }
        else {
            return RosterItem.SUB_NONE;
        }
    }
Matt Tucker's avatar
Matt Tucker committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

    public SubType getSubStatus() {
        return subStatus;
    }

    public void setSubStatus(SubType subStatus) {
        this.subStatus = subStatus;
    }

    public AskType getAskStatus() {
        return askStatus;
    }

    public void setAskStatus(AskType askStatus) {
        this.askStatus = askStatus;
    }

    public RecvType getRecvStatus() {
        return recvStatus;
    }

    public void setRecvStatus(RecvType recvStatus) {
        this.recvStatus = recvStatus;
    }

Derek DeMoro's avatar
Derek DeMoro committed
141
    public JID getJid() {
Matt Tucker's avatar
Matt Tucker committed
142 143 144 145 146 147 148 149 150 151 152
        return jid;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
153
    public List<String> getGroups() {
Matt Tucker's avatar
Matt Tucker committed
154 155 156
        return groups;
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
157
    public void setGroups(List<String> groups) {
Matt Tucker's avatar
Matt Tucker committed
158
        if (groups == null) {
Gaston Dombiak's avatar
Gaston Dombiak committed
159
            this.groups = new LinkedList<String>();
Matt Tucker's avatar
Matt Tucker committed
160 161 162 163 164 165
        }
        else {
            this.groups = groups;
        }
    }
}