PageRenderTime 36ms CodeModel.GetById 26ms app.highlight 9ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/clustering/web-infinispan/src/main/java/org/jboss/as/clustering/web/infinispan/SessionMapEntry.java

#
Java | 82 lines | 29 code | 11 blank | 42 comment | 4 complexity | dcf5c15895bf4368729ee8523770da88 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
 1/*
 2 * JBoss, Home of Professional Open Source.
 3 * Copyright 2010, Red Hat Middleware LLC, and individual contributors
 4 * as indicated by the @author tags. See the copyright.txt file in the
 5 * distribution for a full listing of individual contributors.
 6 *
 7 * This is free software; you can redistribute it and/or modify it
 8 * under the terms of the GNU Lesser General Public License as
 9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22package org.jboss.as.clustering.web.infinispan;
23
24import java.util.Map;
25
26import org.jboss.as.clustering.web.DistributableSessionMetadata;
27
28/**
29 * Enumerates the properties of a session to be stored in a cache entry. Provides get/put methods which encapsulate away the
30 * details of the map key.
31 *
32 * @author Paul Ferraro
33 */
34public enum SessionMapEntry {
35    VERSION(Integer.class), TIMESTAMP(Long.class), METADATA(DistributableSessionMetadata.class), ATTRIBUTES(Object.class);
36
37    private Class<?> targetClass;
38
39    private SessionMapEntry(Class<?> targetClass) {
40        this.targetClass = targetClass;
41    }
42
43    /**
44     * Returns the value associated with this atomic map entry.
45     *
46     * @param <T> the value type
47     * @param map an atomic map
48     * @return the entry value
49     */
50    public <T> T get(Map<Object, Object> map) {
51        return this.<T> cast(map.get(this.key()));
52    }
53
54    /**
55     * Add this entry to the specified map if the specified value is non-null.
56     *
57     * @param <T> the value type
58     * @param map an atomic map
59     * @param value the entry value
60     * @return the old entry value, or null if no previous entry existed
61     */
62    public <T> T put(Map<Object, Object> map, Object value) throws IllegalArgumentException {
63        if (value == null)
64            return null;
65
66        if (!this.targetClass.isInstance(value)) {
67            throw InfinispanWebMessages.MESSAGES.invalidMapValue(value.getClass().getName(), this);
68        }
69
70        return this.<T> cast(map.put(this.key(), value));
71    }
72
73    @SuppressWarnings("unchecked")
74    private <T> T cast(Object value) {
75        Class<T> targetClass = (Class<T>) this.targetClass;
76        return (value != null) ? targetClass.cast(value) : null;
77    }
78
79    private Byte key() {
80        return Byte.valueOf((byte) this.ordinal());
81    }
82}