baseprofilelocator.java

来自「jetspeed源代码」· Java 代码 · 共 691 行 · 第 1/2 页

JAVA
691
字号
     * @param The media type parameter for this profile.
     */
    public void setMediaType(String mediaType)
    {
        this.mediaType = mediaType;
    }

    /*
     * Gets the language parameter for this profile.
     * Language values are ISO-639 standard language abbreviations
     * en, fr, de, ...
     *
     * @return The language parameter for this profile.
     */
    public String getLanguage()
    {
        return language;
    }

    /*
     * Sets the language parameter for this profile.
     * Language values are ISO-639 standard language abbreviations
     * en, fr, de, ...
     *
     * @param The language parameter for this profile.
     */
    public void setLanguage(String language)
    {
        this.language = language;
    }

    /*
     * Gets the country code parameter for this profile.
     * Country code values are ISO-3166 standard country code abbreviations.
     * GB, US, FR, CA, DE, ...
     *
     * @return The country code parameter for this profile.
     */
    public String getCountry()
    {
        return country;
    }

    /*
     * Sets the country code parameter for this profile.
     * Country code values are ISO-3166 standard country code abbreviations.
     * GB, US, FR, CA, DE, ...
     *
     * @param The country code parameter for this profile.
     */
    public void setCountry(String country)
    {
        this.country = country;
    }

    /*
     * Gets the user parameter for this profile.
     *
     * @return The user parameter for this profile.
     */
    public JetspeedUser getUser()
    {
        return user;
    }

    public String getUserName()
    {
        if (null == user)
            return userName;

        return user.getUserName();
    }

    /*
     * Sets the user parameter for this profile.
     *
     * @param The user parameter for this profile.
     */
    public void setUser(JetspeedUser user)
    {
        this.user = user;
    }

    /*
     * Gets the role parameter for this profile.
     *
     * @return The role parameter for this profile.
     */
    public Role getRole()
    {
        return role;
    }

    public String getRoleName()
    {
        if (null == role)
            return roleName;

        return role.getName();
    }

    /*
     * Sets the role parameter for this profile.
     *
     * @param The role parameter for this profile.
     */
    public void setRole( Role role )
    {
        this.role = role;
    }

    public void setRoleByName( String roleName )
    {
        try
        {
            Role temp = JetspeedSecurity.getRole(roleName);
            if (null != temp)
            {
                role = temp;
            }
        }
        catch (Exception e)
        {
            logger.error("ProfileLocator: Failed to set Role " + roleName, e);
        }
    }

    /*
     * Gets the group parameter for this profile.
     *
     * @return The group parameter for this profile.
     */
    public Group getGroup()
    {
        return group;
    }

    public String getGroupName()
    {
        if (null == group)
            return groupName;

        return group.getName();
    }

    /*
     * Sets the group parameter for this profile.
     *
     * @param The group parameter for this profile.
     */
    public void setGroup( Group group )
    {
        this.group = group;
    }

    public void setGroupByName( String groupName )
    {
        try
        {
            Group temp = JetspeedSecurity.getGroup(groupName);
            if (null != temp)
            {
                group = temp;
            }
        }
        catch (Exception e)
        {
            logger.error("ProfileLocator: Failed to set Group: " + e);
        }
    }


    /*
     * Comparision Functions. Contributed by Atul Dambalkar
     */

   /**
     * Define equality criteria for ProfileLocator objects.
     * @param obj ProfileLocator object to be compared with.
     */
    public boolean equals(Object obj)
    {
        if( obj == null )
        {
            return false;
        }
        synchronized (obj)
        {
            if ( ! ( obj instanceof ProfileLocator ) )
            {
                return false;
            }

            ProfileLocator locator = (ProfileLocator)obj;

            String name = locator.getName();
            String mediaType = locator.getMediaType();
            String language = locator.getLanguage();
            String country = locator.getCountry();
            Group group = locator.getGroup();
            Role role = locator.getRole();

            return nameEquals(name)
//                   && locator.getId() == id
                   && mediaTypeEquals(mediaType)
                   && languageEquals(language)
                   && countryEquals(country)
                   && userEquals(locator)
                   && groupEquals(group)
                   && roleEquals(role);
        }
    }

    /**
     * Check equality for given User object with this ProfileLocator's User
     * object.
     */
    private boolean userEquals(ProfileLocator locator)
    {
        JetspeedUser user = locator.getUser();
        // if either of reference is null return false.
        if (exclusiveOr(this.user, user))
        {
            return false;
        }
        // check if both are non-nulls
        if (assertNotNull(this.user) && assertNotNull(user))
        {
            return stringEquals(this.user.getUserName(), user.getUserName());
        }
        // can be anonymous user
        return this.anonymous == locator.getAnonymous();
    }

    /**
     * Check equality for given Group object with this ProfileLocator's Group
     * object.
     */
    private boolean groupEquals(Group group)
    {
        // if either of reference is null return false.
        if (exclusiveOr(this.group, group))
        {
            return false;
        }
        // check if both are non-nulls
        if (assertNotNull(this.group) && assertNotNull(group))
        {
            return stringEquals( this.group.getName(), group.getName());
        }
        // both are null
        return true;
    }

    /**
     * Check equality for given Role object with this ProfileLocator's Role
     * object.
     */
    private boolean roleEquals(Role role)
    {
        // if either of reference is null return false.
        if (exclusiveOr(this.role, role))
        {
            return false;
        }
        // check if both are non-nulls
        if (assertNotNull(this.role) && assertNotNull(role))
        {
            return stringEquals(this.role.getName(), role.getName());
        }
        // both are null
        return true;
    }

    /**
     * Check equality for language object with this ProfileLocator's language
     * object.
     */
    private boolean languageEquals(String language)
    {
        return stringEquals(this.language, language);
    }

    /**
     * Check equality for country object with this ProfileLocator's country
     * object.
     */
    private boolean countryEquals(String country)
    {
        return stringEquals(this.country, country);
    }

    /**
     * Check equality for name object with this ProfileLocator's name
     * object.
     */
    private boolean nameEquals(String name)
    {
        return stringEquals(this.name, name);
    }

    /**
     * Check equality for name object with this ProfileLocator's name
     * object.
     */
    private boolean mediaTypeEquals(String mediaType)
    {
        return stringEquals(this.mediaType, mediaType);
    }

    /**
     * AssertNotNull the two String objects and then check the equality.
     */
    private boolean stringEquals(String str1, String str2)
    {
        if (exclusiveOr(str1, str2))
        {
            return false;
        }
        if (assertNotNull(str1) && assertNotNull(str2))
        {
            return str1.equals(str2);
        }
        // both are null
        return true;
    }

    /**
     * AssertNotNull the given object.
     */
    private boolean assertNotNull(Object object)
    {
        return object != null;
    }

    /**
     * Exclusive or the two objects fro their references null-ability.
     */
    private boolean exclusiveOr(Object obj1, Object obj2)
    {
        return (assertNotNull(obj1) && !assertNotNull(obj2))
                || (!assertNotNull(obj1) && assertNotNull(obj2));
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?