-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SynchronizedMutable; Mutables utility class
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
de.metas.util/src/main/java/org/adempiere/util/lang/Mutables.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.adempiere.util.lang; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
/* | ||
* #%L | ||
* de.metas.util | ||
* %% | ||
* Copyright (C) 2017 metas GmbH | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 2 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-2.0.html>. | ||
* #L% | ||
*/ | ||
|
||
@UtilityClass | ||
public class Mutables | ||
{ | ||
public static <T> IMutable<T> of(final T value) | ||
{ | ||
return new Mutable<>(value); | ||
} | ||
|
||
public static <T> SynchronizedMutable<T> synchronizedMutable() | ||
{ | ||
return SynchronizedMutable.of(null); | ||
} | ||
|
||
public static <T> SynchronizedMutable<T> synchronizedMutable(final T value) | ||
{ | ||
return SynchronizedMutable.of(value); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
de.metas.util/src/main/java/org/adempiere/util/lang/SynchronizedMutable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.adempiere.util.lang; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import lombok.NonNull; | ||
|
||
/* | ||
* #%L | ||
* de.metas.util | ||
* %% | ||
* Copyright (C) 2017 metas GmbH | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 2 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-2.0.html>. | ||
* #L% | ||
*/ | ||
|
||
public final class SynchronizedMutable<T> implements IMutable<T> | ||
{ | ||
public static <T> SynchronizedMutable<T> of(final T value) | ||
{ | ||
return new SynchronizedMutable<>(value); | ||
} | ||
|
||
private T value; | ||
|
||
private SynchronizedMutable(final T value) | ||
{ | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public synchronized T getValue() | ||
{ | ||
return value; | ||
} | ||
|
||
@Override | ||
public synchronized void setValue(final T value) | ||
{ | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public synchronized T compute(@NonNull final Function<T, T> remappingFunction) | ||
{ | ||
this.value = remappingFunction.apply(this.value); | ||
return this.value; | ||
} | ||
|
||
public synchronized T computeIfNull(@NonNull final Supplier<T> supplier) | ||
{ | ||
if (value == null) | ||
{ | ||
this.value = supplier.get(); | ||
} | ||
|
||
return this.value; | ||
} | ||
|
||
public synchronized T computeIfNotNull(@NonNull final Function<T, T> remappingFunction) | ||
{ | ||
if (value != null) | ||
{ | ||
this.value = remappingFunction.apply(this.value); | ||
} | ||
|
||
return this.value; | ||
} | ||
|
||
} |
ed28bab
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right task is metasfresh/metasfresh-webui-api-legacy#751