PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms 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. */
  22. package org.jboss.as.clustering.web.infinispan;
  23. import java.util.Map;
  24. import org.jboss.as.clustering.web.DistributableSessionMetadata;
  25. /**
  26. * Enumerates the properties of a session to be stored in a cache entry. Provides get/put methods which encapsulate away the
  27. * details of the map key.
  28. *
  29. * @author Paul Ferraro
  30. */
  31. public enum SessionMapEntry {
  32. VERSION(Integer.class), TIMESTAMP(Long.class), METADATA(DistributableSessionMetadata.class), ATTRIBUTES(Object.class);
  33. private Class<?> targetClass;
  34. private SessionMapEntry(Class<?> targetClass) {
  35. this.targetClass = targetClass;
  36. }
  37. /**
  38. * Returns the value associated with this atomic map entry.
  39. *
  40. * @param <T> the value type
  41. * @param map an atomic map
  42. * @return the entry value
  43. */
  44. public <T> T get(Map<Object, Object> map) {
  45. return this.<T> cast(map.get(this.key()));
  46. }
  47. /**
  48. * Add this entry to the specified map if the specified value is non-null.
  49. *
  50. * @param <T> the value type
  51. * @param map an atomic map
  52. * @param value the entry value
  53. * @return the old entry value, or null if no previous entry existed
  54. */
  55. public <T> T put(Map<Object, Object> map, Object value) throws IllegalArgumentException {
  56. if (value == null)
  57. return null;
  58. if (!this.targetClass.isInstance(value)) {
  59. throw InfinispanWebMessages.MESSAGES.invalidMapValue(value.getClass().getName(), this);
  60. }
  61. return this.<T> cast(map.put(this.key(), value));
  62. }
  63. @SuppressWarnings("unchecked")
  64. private <T> T cast(Object value) {
  65. Class<T> targetClass = (Class<T>) this.targetClass;
  66. return (value != null) ? targetClass.cast(value) : null;
  67. }
  68. private Byte key() {
  69. return Byte.valueOf((byte) this.ordinal());
  70. }
  71. }