/*
 * Copyright 2014 OpenMarket Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.matrix.androidsdk.rest.model;

/**
 * Class representing a room member: a user with membership information.
 */
public class RoomMember implements java.io.Serializable {
    public static final String MEMBERSHIP_JOIN = "join";
    public static final String MEMBERSHIP_INVITE = "invite";
    public static final String MEMBERSHIP_LEAVE = "leave";
    public static final String MEMBERSHIP_BAN = "ban";

    public String displayname;
    public String avatarUrl;
    public String membership;

    private String userId;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    private Boolean fieldsAreEqual(String s1, String s2) {
        // compare display name
        if (s1 == null) {
            return (s1 == s2);
        } else {
            return s1.equals(s2);
        }
    }

    /**
     * Compare two members.
     * The members are equals if each field have the same value.
     * @param otherMember the member to compare.
     * @return true if they define the same member.
     */
    public boolean equals(RoomMember otherMember) {
        // compare to null
        if (null == otherMember) {
            return false;
        }

        // compare display name
        Boolean isEqual = fieldsAreEqual(displayname, otherMember.displayname);

        if (isEqual) {
            isEqual = fieldsAreEqual(avatarUrl, otherMember.avatarUrl);
        }

        if (isEqual) {
            isEqual = fieldsAreEqual(membership, otherMember.membership);
        }

        if (isEqual) {
            isEqual = fieldsAreEqual(userId, otherMember.userId);
        }

        return isEqual;
    }

    public String getName() {
        if (displayname != null) {
            return displayname;
        }
        if (userId != null) {
            return userId;
        }
        return null;
    }

    public RoomMember deepCopy() {
        RoomMember copy = new RoomMember();
        copy.displayname = displayname;
        copy.avatarUrl = avatarUrl;
        copy.membership = membership;
        copy.userId = userId;
        return copy;
    }

    public boolean hasLeft() {
        return RoomMember.MEMBERSHIP_BAN.equals(membership) || RoomMember.MEMBERSHIP_LEAVE.equals(membership);
    }
}

@@ -634,16 +634,110 @@
 model;%0A%0A
+import android.text.TextUtils;%0A%0Aimport java.util.Comparator;%0Aimport java.util.regex.Pattern;%0A%0A
 /**%0A * C
@@ -1573,24 +1573,1461 @@
    %7D%0A    %7D%0A%0A
+    // Comparator to order members alphabetically%0A    public static Comparator%3CRoomMember%3E alphaComparator = new Comparator%3CRoomMember%3E() %7B%0A        @Override%0A        public int compare(RoomMember member1, RoomMember member2) %7B%0A            String lhs = member1.getName();%0A            String rhs = member2.getName();%0A%0A            if (lhs == null) %7B%0A                return -1;%0A            %7D%0A            else if (rhs == null) %7B%0A                return 1;%0A            %7D%0A            if (lhs.startsWith(%22@%22)) %7B%0A                lhs = lhs.substring(1);%0A            %7D%0A            if (rhs.startsWith(%22@%22)) %7B%0A                rhs = rhs.substring(1);%0A            %7D%0A            return String.CASE_INSENSITIVE_ORDER.compare(lhs, rhs);%0A        %7D%0A    %7D;%0A%0A    /**%0A     * Test if a room memmber matches with a pattern.%0A     * The check is done with the displayname and the userId.%0A     * @param aPattern the pattern to search.%0A     * @return true if it matches.%0A     */%0A    public boolean matchWith(String aPattern) %7B%0A        if (TextUtils.isEmpty(aPattern) %7C%7C TextUtils.isEmpty(aPattern.trim())) %7B%0A            return false;%0A        %7D%0A%0A        String regEx = %22(?i:.*%22 + aPattern.trim() + %22.*)%22;%0A        boolean res = false;%0A%0A        if (!TextUtils.isEmpty(displayname)) %7B%0A            res = displayname.matches(regEx);%0A        %7D%0A%0A        if (!res && !TextUtils.isEmpty(userId)) %7B%0A            res = userId.matches(regEx);%0A        %7D%0A%0A        return res;%0A    %7D%0A%0A
     /**%0A    

Add matchWith and sort methods.