![JAR search and dependency download from the Maven repository](/logo.png)
de.carne.jfx.util.DefaultSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-jfx Show documentation
Show all versions of java-jfx Show documentation
Common JavaFX code shared between my projects
The newest version!
/*
* Copyright (c) 2016-2020 Holger de Carne and contributors, All Rights Reserved.
*
* 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 3 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 .
*/
package de.carne.jfx.util;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import org.eclipse.jdt.annotation.Nullable;
/**
* Utility class combining a result set with a corresponding default value.
*
* @param The set element type.
*/
public final class DefaultSet extends HashSet {
/*
* Serialization support.
*/
private static final long serialVersionUID = 2969592903102269986L;
@Nullable
private T defaultEntry = null;
/**
* Construct {@code DefaultSet}.
*/
public DefaultSet() {
super();
}
/**
* Construct {@code DefaultSet}.
*
* @param elements The initial set elements.
*/
public DefaultSet(Collection extends T> elements) {
super(elements);
Iterator extends T> iterator = elements.iterator();
if (iterator.hasNext()) {
addDefault(iterator.next());
}
}
@Override
public boolean add(@Nullable T entry) {
if (this.defaultEntry == null) {
this.defaultEntry = entry;
}
return super.add(entry);
}
@Override
public void clear() {
this.defaultEntry = null;
super.clear();
}
/**
* Add an entry to the set and make it the default.
*
* @param entry The default entry.
*/
public void addDefault(@Nullable T entry) {
add(entry);
this.defaultEntry = entry;
}
/**
* Get the default entry.
*
* @return The default entry.
*/
@Nullable
public T getDefault() {
return this.defaultEntry;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy