AbstractVariables.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /**
  23. * @author UnAfraid
  24. */
  25. public abstract class AbstractVariables extends StatsSet
  26. {
  27. private final AtomicBoolean _hasChanges = new AtomicBoolean(false);
  28. protected abstract void load();
  29. public abstract void store();
  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 {@code true} if changes are made since last load/save.
  71. */
  72. public final boolean hasChanges()
  73. {
  74. return _hasChanges.get();
  75. }
  76. /**
  77. * Atomically sets the value to the given updated value if the current value {@code ==} the expected value.
  78. * @param expect
  79. * @param update
  80. * @return {@code true} if successful. {@code false} return indicates that the actual value was not equal to the expected value.
  81. */
  82. public final boolean compareAndSetChanges(boolean expect, boolean update)
  83. {
  84. return _hasChanges.compareAndSet(expect, update);
  85. }
  86. }