AbstractVariables.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2004-2013 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 com.l2jserver.gameserver.model.StatsSet;
  22. import com.l2jserver.gameserver.model.interfaces.IRestorable;
  23. import com.l2jserver.gameserver.model.interfaces.IStorable;
  24. /**
  25. * @author UnAfraid
  26. */
  27. public abstract class AbstractVariables extends StatsSet implements IRestorable, IStorable
  28. {
  29. private final AtomicBoolean _hasChanges = new AtomicBoolean(false);
  30. /**
  31. * Overriding following methods to prevent from doing useless database operations if there is no changes since player's login.
  32. */
  33. @Override
  34. public final void set(String name, boolean value)
  35. {
  36. _hasChanges.compareAndSet(false, true);
  37. super.set(name, value);
  38. }
  39. @Override
  40. public final void set(String name, double value)
  41. {
  42. _hasChanges.compareAndSet(false, true);
  43. super.set(name, value);
  44. }
  45. @Override
  46. public final void set(String name, Enum<?> value)
  47. {
  48. _hasChanges.compareAndSet(false, true);
  49. super.set(name, value);
  50. }
  51. @Override
  52. public final void set(String name, int value)
  53. {
  54. _hasChanges.compareAndSet(false, true);
  55. super.set(name, value);
  56. }
  57. @Override
  58. public final void set(String name, long value)
  59. {
  60. _hasChanges.compareAndSet(false, true);
  61. super.set(name, value);
  62. }
  63. @Override
  64. public final void set(String name, String value)
  65. {
  66. _hasChanges.compareAndSet(false, true);
  67. super.set(name, value);
  68. }
  69. /**
  70. * Return true if there exists a record for the variable name.
  71. * @param name
  72. * @return
  73. */
  74. public boolean hasVariable(String name)
  75. {
  76. return getSet().keySet().contains(name);
  77. }
  78. /**
  79. * @return {@code true} if changes are made since last load/save.
  80. */
  81. public final boolean hasChanges()
  82. {
  83. return _hasChanges.get();
  84. }
  85. /**
  86. * Atomically sets the value to the given updated value if the current value {@code ==} the expected value.
  87. * @param expect
  88. * @param update
  89. * @return {@code true} if successful. {@code false} return indicates that the actual value was not equal to the expected value.
  90. */
  91. public final boolean compareAndSetChanges(boolean expect, boolean update)
  92. {
  93. return _hasChanges.compareAndSet(expect, update);
  94. }
  95. }