AbstractVariables.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.variables;
  20. import java.util.concurrent.atomic.AtomicBoolean;
  21. import javolution.util.FastMap;
  22. import com.l2jserver.gameserver.model.StatsSet;
  23. import com.l2jserver.gameserver.model.interfaces.IRestorable;
  24. import com.l2jserver.gameserver.model.interfaces.IStorable;
  25. /**
  26. * @author UnAfraid
  27. */
  28. public abstract class AbstractVariables extends StatsSet implements IRestorable, IStorable
  29. {
  30. private final AtomicBoolean _hasChanges = new AtomicBoolean(false);
  31. public AbstractVariables()
  32. {
  33. super(new FastMap<String, Object>().shared());
  34. }
  35. /**
  36. * Overriding following methods to prevent from doing useless database operations if there is no changes since player's login.
  37. */
  38. @Override
  39. public final void set(String name, boolean value)
  40. {
  41. _hasChanges.compareAndSet(false, true);
  42. super.set(name, value);
  43. }
  44. @Override
  45. public final void set(String name, double value)
  46. {
  47. _hasChanges.compareAndSet(false, true);
  48. super.set(name, value);
  49. }
  50. @Override
  51. public final void set(String name, Enum<?> value)
  52. {
  53. _hasChanges.compareAndSet(false, true);
  54. super.set(name, value);
  55. }
  56. @Override
  57. public final void set(String name, int value)
  58. {
  59. _hasChanges.compareAndSet(false, true);
  60. super.set(name, value);
  61. }
  62. @Override
  63. public final void set(String name, long value)
  64. {
  65. _hasChanges.compareAndSet(false, true);
  66. super.set(name, value);
  67. }
  68. @Override
  69. public final void set(String name, String value)
  70. {
  71. _hasChanges.compareAndSet(false, true);
  72. super.set(name, value);
  73. }
  74. /**
  75. * Return true if there exists a record for the variable name.
  76. * @param name
  77. * @return
  78. */
  79. public boolean hasVariable(String name)
  80. {
  81. return getSet().keySet().contains(name);
  82. }
  83. /**
  84. * @return {@code true} if changes are made since last load/save.
  85. */
  86. public final boolean hasChanges()
  87. {
  88. return _hasChanges.get();
  89. }
  90. /**
  91. * Atomically sets the value to the given updated value if the current value {@code ==} the expected value.
  92. * @param expect
  93. * @param update
  94. * @return {@code true} if successful. {@code false} return indicates that the actual value was not equal to the expected value.
  95. */
  96. public final boolean compareAndSetChanges(boolean expect, boolean update)
  97. {
  98. return _hasChanges.compareAndSet(expect, update);
  99. }
  100. /**
  101. * Removes variable
  102. * @param name
  103. */
  104. public final void remove(String name)
  105. {
  106. _hasChanges.compareAndSet(false, true);
  107. getSet().remove(name);
  108. }
  109. }