<commit_before>package org.cytoscape.ding.impl;

import java.awt.Color;
import java.awt.Paint;
import java.util.IdentityHashMap;
import java.util.Map;

import org.cytoscape.model.SUIDFactory;
import org.cytoscape.view.model.View;
import org.cytoscape.view.model.VisualProperty;

public abstract class AbstractDViewModel<M> implements View<M> {

	// Both of them are immutable.
	protected final M model;
	protected final Long suid;

	protected final Map<VisualProperty<?>, Object> visualProperties;
	protected final Map<VisualProperty<?>, Object> visualPropertyLocks;

	/**
	 * Create an instance of view model, but not firing event to upper layer.
	 * 
	 * @param model
	 */
	public AbstractDViewModel(final M model) {
		if (model == null)
			throw new IllegalArgumentException("Data model cannot be null.");

		this.suid = Long.valueOf(SUIDFactory.getNextSUID());
		this.model = model;

		this.visualProperties = new IdentityHashMap<VisualProperty<?>, Object>();
		this.visualPropertyLocks = new IdentityHashMap<VisualProperty<?>, Object>();
	}

	@Override
	public M getModel() {
		return model;
	}

	@Override
	public Long getSUID() {
		return suid;
	}

	@Override
	public <T, V extends T> void setVisualProperty(final VisualProperty<? extends T> vp, V value) {
		if (value == null)
			visualProperties.remove(vp);
		else
			visualProperties.put(vp, value);

		if (!isValueLocked(vp))
			applyVisualProperty(vp, value);
	}

	@Override
	public <T, V extends T> void setLockedValue(final VisualProperty<? extends T> vp, final V value) {
		visualPropertyLocks.put(vp, value);
		applyVisualProperty(vp, value);
	}

	@Override
	public boolean isValueLocked(final VisualProperty<?> vp) {
		return visualPropertyLocks.get(vp) != null;
	}

	@Override
	public void clearValueLock(final VisualProperty<?> vp) {
		visualPropertyLocks.remove(vp);
		
		// Re-apply the regular visual property value
		if (visualProperties.containsKey(vp))
			applyVisualProperty(vp, visualProperties.get(vp));
		// TODO else: reset to the visual style default if visualProperties map doesn't contain this vp
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T getVisualProperty(final VisualProperty<T> vp) {
		if (visualPropertyLocks.get(vp) == null) {
			if (visualProperties.get(vp) == null) {
				// Mapped value is null.  Try default
				final T viewDefault = this.getDefaultValue(vp);
				if(viewDefault != null)
					return viewDefault;
				else
					return vp.getDefault();
			} else
				return (T) visualProperties.get(vp);
		} else {
			// Visual Property is LOCKED.  Return Locked value.
			return (T) this.visualPropertyLocks.get(vp);
		}
	}
	
	@Override
	public boolean isSet(final VisualProperty<?> vp) {
		return visualProperties.get(vp) != null || visualPropertyLocks.get(vp) != null || getDefaultValue(vp) != null;
	}
	
	protected abstract <T, V extends T> void applyVisualProperty(final VisualProperty<? extends T> vp, V value);
	protected abstract <T, V extends T> V getDefaultValue(final VisualProperty<T> vp);
	
	protected final Paint getTransparentColor(final Paint p, final int alpha) {
		if(p == null)
			return p;
		
		if (p instanceof Color && ((Color) p).getAlpha() != alpha) {
			final Color c = (Color) p;
			return new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);
		} else {
			return p;
		}
	}
}
<commit_msg>fixes #1391 Default value will be applied after lock value clear method call.<commit_after>package org.cytoscape.ding.impl;

import java.awt.Color;
import java.awt.Paint;
import java.util.IdentityHashMap;
import java.util.Map;

import org.cytoscape.model.SUIDFactory;
import org.cytoscape.view.model.View;
import org.cytoscape.view.model.VisualProperty;

public abstract class AbstractDViewModel<M> implements View<M> {

	// Both of them are immutable.
	protected final M model;
	protected final Long suid;

	protected final Map<VisualProperty<?>, Object> visualProperties;
	protected final Map<VisualProperty<?>, Object> visualPropertyLocks;

	/**
	 * Create an instance of view model, but not firing event to upper layer.
	 * 
	 * @param model
	 */
	public AbstractDViewModel(final M model) {
		if (model == null)
			throw new IllegalArgumentException("Data model cannot be null.");

		this.suid = Long.valueOf(SUIDFactory.getNextSUID());
		this.model = model;

		this.visualProperties = new IdentityHashMap<VisualProperty<?>, Object>();
		this.visualPropertyLocks = new IdentityHashMap<VisualProperty<?>, Object>();
	}

	@Override
	public M getModel() {
		return model;
	}

	@Override
	public Long getSUID() {
		return suid;
	}

	@Override
	public <T, V extends T> void setVisualProperty(final VisualProperty<? extends T> vp, V value) {
		if (value == null)
			visualProperties.remove(vp);
		else
			visualProperties.put(vp, value);

		if (!isValueLocked(vp))
			applyVisualProperty(vp, value);
	}

	@Override
	public <T, V extends T> void setLockedValue(final VisualProperty<? extends T> vp, final V value) {
		visualPropertyLocks.put(vp, value);
		applyVisualProperty(vp, value);
	}

	@Override
	public boolean isValueLocked(final VisualProperty<?> vp) {
		return visualPropertyLocks.get(vp) != null;
	}

	@Override
	public void clearValueLock(final VisualProperty<?> vp) {
		visualPropertyLocks.remove(vp);
		// Re-apply the regular visual property value
		if (visualProperties.containsKey(vp)) {
			applyVisualProperty(vp, visualProperties.get(vp));
		// TODO else: reset to the visual style default if visualProperties map doesn't contain this vp
		} else {
			// Apply default if necessary.
			final Object newValue = getVisualProperty(vp);
			applyVisualProperty(vp, newValue);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T getVisualProperty(final VisualProperty<T> vp) {
		if (visualPropertyLocks.get(vp) == null) {
			if (visualProperties.get(vp) == null) {
				// Mapped value is null.  Try default
				final T viewDefault = this.getDefaultValue(vp);
				if(viewDefault != null)
					return viewDefault;
				else
					return vp.getDefault();
			} else
				return (T) visualProperties.get(vp);
		} else {
			// Visual Property is LOCKED.  Return Locked value.
			return (T) this.visualPropertyLocks.get(vp);
		}
	}
	
	@Override
	public boolean isSet(final VisualProperty<?> vp) {
		return visualProperties.get(vp) != null || visualPropertyLocks.get(vp) != null || getDefaultValue(vp) != null;
	}
	
	protected abstract <T, V extends T> void applyVisualProperty(final VisualProperty<? extends T> vp, V value);
	protected abstract <T, V extends T> V getDefaultValue(final VisualProperty<T> vp);
	
	protected final Paint getTransparentColor(final Paint p, final int alpha) {
		if(p == null)
			return p;
		
		if (p instanceof Color && ((Color) p).getAlpha() != alpha) {
			final Color c = (Color) p;
			return new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);
		} else {
			return p;
		}
	}
}

{'fraction_non_alphanumeric': 0.11595682613768961, 'fraction_numerical': 0.0005834305717619603, 'mean_word_length': 4.299072642967542, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '835472', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2112, 'n_words': 625}