001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.cache.cluster;
016    
017    import com.liferay.portal.kernel.cache.PortalCache;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.Serializable;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class PortalCacheClusterEvent implements Serializable {
028    
029            public PortalCacheClusterEvent(
030                    String portalCacheManagerName, String portalCacheName,
031                    Serializable elementKey,
032                    PortalCacheClusterEventType portalCacheClusterEventType) {
033    
034                    this(
035                            portalCacheManagerName, portalCacheName, elementKey, null,
036                            PortalCache.DEFAULT_TIME_TO_LIVE, portalCacheClusterEventType);
037            }
038    
039            public PortalCacheClusterEvent(
040                    String portalCacheManagerName, String portalCacheName,
041                    Serializable elementKey, Serializable elementValue, int timeToLive,
042                    PortalCacheClusterEventType portalCacheClusterEventType) {
043    
044                    if (portalCacheManagerName == null) {
045                            throw new NullPointerException("Portal cache manager name is null");
046                    }
047    
048                    if (portalCacheName == null) {
049                            throw new NullPointerException("Portal cache name is null");
050                    }
051    
052                    if (portalCacheClusterEventType == null) {
053                            throw new NullPointerException(
054                                    "Portal cache cluster event type is null");
055                    }
056    
057                    if ((elementKey == null) &&
058                            !portalCacheClusterEventType.equals(
059                                    PortalCacheClusterEventType.REMOVE_ALL)) {
060    
061                            throw new NullPointerException("Element key is null");
062                    }
063    
064                    if (timeToLive < 0) {
065                            throw new IllegalArgumentException("Time to live is negative");
066                    }
067    
068                    _portalCacheManagerName = portalCacheManagerName;
069                    _portalCacheName = portalCacheName;
070                    _elementKey = elementKey;
071                    _elementValue = elementValue;
072                    _timeToLive = timeToLive;
073                    _portalCacheClusterEventType = portalCacheClusterEventType;
074            }
075    
076            @Override
077            public boolean equals(Object obj) {
078                    if (this == obj) {
079                            return true;
080                    }
081    
082                    if (!(obj instanceof PortalCacheClusterEvent)) {
083                            return false;
084                    }
085    
086                    PortalCacheClusterEvent portalCacheClusterEvent =
087                            (PortalCacheClusterEvent)obj;
088    
089                    if (Validator.equals(
090                                    _elementKey, portalCacheClusterEvent._elementKey) &&
091                            Validator.equals(
092                                    _elementValue, portalCacheClusterEvent._elementValue) &&
093                            Validator.equals(
094                                    _portalCacheClusterEventType,
095                                    portalCacheClusterEvent._portalCacheClusterEventType) &&
096                            Validator.equals(
097                                    _portalCacheManagerName,
098                                    portalCacheClusterEvent._portalCacheManagerName) &&
099                            Validator.equals(
100                                    _portalCacheName, portalCacheClusterEvent._portalCacheName)) {
101    
102                            return true;
103                    }
104    
105                    return false;
106            }
107    
108            public Serializable getElementKey() {
109                    return _elementKey;
110            }
111    
112            public Serializable getElementValue() {
113                    return _elementValue;
114            }
115    
116            public PortalCacheClusterEventType getEventType() {
117                    return _portalCacheClusterEventType;
118            }
119    
120            public String getPortalCacheManagerName() {
121                    return _portalCacheManagerName;
122            }
123    
124            public String getPortalCacheName() {
125                    return _portalCacheName;
126            }
127    
128            public int getTimeToLive() {
129                    return _timeToLive;
130            }
131    
132            @Override
133            public int hashCode() {
134                    return toString().hashCode();
135            }
136    
137            public void setElementValue(Serializable elementValue) {
138                    _elementValue = elementValue;
139            }
140    
141            public void setTimeToLive(int timeToLive) {
142                    if (timeToLive < 0) {
143                            throw new IllegalArgumentException("Time to live is negative");
144                    }
145    
146                    _timeToLive = timeToLive;
147            }
148    
149            @Override
150            public String toString() {
151                    StringBundler sb = new StringBundler(9);
152    
153                    sb.append(_portalCacheManagerName);
154                    sb.append(StringPool.COLON);
155                    sb.append(_portalCacheName);
156                    sb.append(StringPool.COLON);
157                    sb.append(_elementKey);
158                    sb.append(StringPool.COLON);
159    
160                    if (_elementValue != null) {
161                            sb.append(_elementValue.toString());
162                            sb.append(StringPool.COLON);
163                    }
164    
165                    sb.append(_portalCacheClusterEventType.toString());
166    
167                    return sb.toString();
168            }
169    
170            private final Serializable _elementKey;
171            private Serializable _elementValue;
172            private final PortalCacheClusterEventType _portalCacheClusterEventType;
173            private final String _portalCacheManagerName;
174            private final String _portalCacheName;
175            private int _timeToLive;
176    
177    }