1 /* 2 * ************************************************************************************************************************************************************* 3 * 4 * TheseFoolishThings: Miscellaneous utilities 5 * http://tidalwave.it/projects/thesefoolishthings 6 * 7 * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it) 8 * 9 * ************************************************************************************************************************************************************* 10 * 11 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 12 * You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 18 * 19 * ************************************************************************************************************************************************************* 20 * 21 * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src 22 * git clone https://github.com/tidalwave-it/thesefoolishthings-src 23 * 24 * ************************************************************************************************************************************************************* 25 */ 26 package it.tidalwave.role.ui.function; 27 28 import javax.annotation.Nonnull; 29 import it.tidalwave.role.ui.Changeable; 30 import it.tidalwave.role.ui.ChangingSource; 31 import lombok.extern.slf4j.Slf4j; 32 33 /*************************************************************************************************************************************************************** 34 * 35 * Changes the destination only at a certain condition in function of the target. 36 * 37 * @author Fabrizio Giudici 38 * 39 **************************************************************************************************************************************************************/ 40 @Slf4j 41 public abstract class WeakCopyFunctionSupport<T> extends UnaryBoundFunctionSupport<T, T> implements Changeable<T> 42 { 43 @Nonnull 44 protected T targetValue; 45 46 public WeakCopyFunctionSupport (@Nonnull final ChangingSource<T> source) 47 { 48 super(source); 49 } 50 51 @Override 52 protected void onSourceChange (@Nonnull final T oldSourceValue, @Nonnull final T newSourceValue) 53 { 54 final var oldValue = function(oldSourceValue); 55 final var newValue = function(newSourceValue); 56 57 if (shouldChange(oldValue, newValue)) 58 { 59 value = newValue; 60 fireValueChanged(oldValue, newValue); 61 } 62 } 63 64 @Override 65 public void set (final T value) 66 { 67 this.targetValue = value; 68 } 69 70 protected abstract boolean shouldChange (T oldValue, T newValue); 71 72 @Override @Nonnull 73 protected T function (@Nonnull final T value) 74 { 75 return value; 76 } 77 }