Skip to content

Commit

Permalink
SynchronizedMutable; Mutables utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Dec 15, 2017
1 parent cdd54c0 commit ed28bab
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
44 changes: 44 additions & 0 deletions de.metas.util/src/main/java/org/adempiere/util/lang/Mutables.java
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);
}
}
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;
}

}

1 comment on commit ed28bab

@teosarca
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.