All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.qt.internal.QtJambiMapObject Maven / Gradle / Ivy

The newest version!
/****************************************************************************
**
** Copyright (C) 2009-2022 Dr. Peter Droste, Omix Visualization GmbH & Co. KG. All rights reserved.
**
** This file is part of Qt Jambi.
**
** ** $BEGIN_LICENSE$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** 
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
** $END_LICENSE$

**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
package io.qt.internal;

import java.util.AbstractMap;
import java.util.Comparator;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import io.qt.QtUninvokable;
import io.qt.core.QPair;

public abstract class QtJambiMapObject extends QtJambiAbstractMapObject implements NavigableMap {

	protected QtJambiMapObject() {
		super();
	}

    protected QtJambiMapObject(QPrivateConstructor p) {
		super(p);
	}
    
    @QtUninvokable
    protected abstract QtJambiMapIteratorObject find(K key);
    
    @QtUninvokable
    protected abstract QtJambiMapIteratorObject lowerBound(K key);

    @QtUninvokable
    protected abstract QtJambiMapIteratorObject upperBound(K key);

	@Override
    @QtUninvokable
	public final NavigableMap subMap(K fromKey, K toKey) {
		return subMap(fromKey, true, toKey, false);
	}

	@Override
    @QtUninvokable
	public final NavigableMap headMap(K toKey) {
		return headMap(toKey, false);
	}

	@Override
    @QtUninvokable
	public final NavigableMap tailMap(K fromKey) {
		return tailMap(fromKey, true);
	}

    @Override
    @QtUninvokable
	public final Entry lowerEntry(K key) {
    	QtJambiMapIteratorObject iterator = lowerBound(key);
    	if(!iterator.equals(end()) && !iterator.equals(begin()) && Objects.equals(iterator.checkedKey(), key))
    		iterator.decrement();
    	if(iterator.equals(end()))
    		return null;
    	else
    		return new AbstractMap.SimpleImmutableEntry<>(iterator.checkedKey(), iterator.checkedValue());
	}

	@Override
    @QtUninvokable
	public final K lowerKey(K key) {
    	QtJambiMapIteratorObject iterator = lowerBound(key);
    	if(!iterator.equals(end()) && !iterator.equals(begin()) && Objects.equals(iterator.checkedKey(), key))
    		iterator.decrement();
    	if(iterator.equals(end()))
    		return null;
    	else return iterator.checkedKey();
	}

	@Override
    @QtUninvokable
	public final Entry floorEntry(K key) {
    	QtJambiMapIteratorObject iterator = lowerBound(key);
    	if(iterator.equals(end()))
    		return null;
    	else
    		return new AbstractMap.SimpleImmutableEntry<>(iterator.checkedKey(), iterator.checkedValue());
	}

	@Override
    @QtUninvokable
	public final K floorKey(K key) {
    	QtJambiMapIteratorObject iterator = lowerBound(key);
    	if(iterator.equals(end()))
    		return null;
    	else return iterator.checkedKey();
	}

	@Override
    @QtUninvokable
	public final Entry ceilingEntry(K key) {
    	QtJambiMapIteratorObject iterator = lowerBound(key);
    	if(iterator.equals(end()))
    		return null;
    	else
    		return new AbstractMap.SimpleImmutableEntry<>(iterator.checkedKey(), iterator.checkedValue());
	}

	@Override
    @QtUninvokable
	public final K ceilingKey(K key) {
    	QtJambiMapIteratorObject iterator = lowerBound(key);
    	if(iterator.equals(end()))
    		return null;
    	else return iterator.checkedKey();
	}

	@Override
    @QtUninvokable
	public final Entry higherEntry(K key) {
    	QtJambiMapIteratorObject iterator = lowerBound(key);
    	if(!iterator.equals(end()) && Objects.equals(iterator.checkedKey(), key))
    		iterator.increment();
    	if(iterator.equals(end()))
    		return null;
    	else
    		return new AbstractMap.SimpleImmutableEntry<>(iterator.checkedKey(), iterator.checkedValue());
	}

	@Override
    @QtUninvokable
	public final K higherKey(K key) {
    	QtJambiMapIteratorObject iterator = lowerBound(key);
    	if(!iterator.equals(end()) && Objects.equals(iterator.checkedKey(), key))
    		iterator.increment();
    	if(iterator.equals(end()))
    		return null;
    	else
    		return iterator.checkedKey();
	}

	@Override
    @QtUninvokable
	public final Entry firstEntry() {
		if(isEmpty())
			return null;
		else return new AbstractMap.SimpleImmutableEntry<>(begin().checkedKey(), begin().checkedValue());
	}

	@Override
    @QtUninvokable
	public final Entry lastEntry() {
		if(isEmpty())
			return null;
		else {
			QtJambiMapIteratorObject end = end();
			end.decrement();
			return new AbstractMap.SimpleImmutableEntry<>(end.checkedKey(), end.checkedValue());
		}
	}

	@Override
    @QtUninvokable
	public final Entry pollFirstEntry() {
		Entry entry = firstEntry();
		if(entry!=null) {
			remove(entry.getKey());
		}
		return entry;
	}

	@Override
    @QtUninvokable
	public final Entry pollLastEntry() {
		Entry entry = lastEntry();
		if(entry!=null) {
			remove(entry.getKey());
		}
		return entry;
	}

	@Override
    @QtUninvokable
	public final NavigableMap descendingMap() {
		Comparator comparator = this.comparator();
		TreeMap descendingMap = new TreeMap<>((K o1, K o2)-> -1*comparator.compare(o1, o2));
		descendingMap.putAll(this);
		return descendingMap;
	}

	@Override
    @QtUninvokable
	public final NavigableSet navigableKeySet() {
		TreeSet set = new TreeSet<>(this.comparator());
		set.addAll(this.keySet());
		return set;
	}

	@Override
    @QtUninvokable
	public final NavigableSet descendingKeySet() {
		Comparator comparator = this.comparator();
		TreeSet set = new TreeSet<>((K o1, K o2)-> -1*comparator.compare(o1, o2));
		set.addAll(this.keySet());
		return set;
	}

	@SuppressWarnings("unchecked")
	@Override
    @QtUninvokable
	public final NavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
		NavigableMap map;
		try {
			map = this.getClass().getConstructor().newInstance();
		} catch (Throwable e) {
			map = new TreeMap<>(comparator());
		}
		QtJambiMapIteratorObject k2 = lowerBound(toKey);
		if(!k2.equals(end())) {
			QtJambiMapIteratorObject k1 = lowerBound(fromKey);
			if(!fromInclusive) {
				k1.increment();
			}
			for(; !k1.equals(k2); k1.increment()) {
				map.put(k1.checkedKey(), k1.checkedValue());
			}
			if(toInclusive) {
				map.put(k2.checkedKey(), k2.checkedValue());
			}
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
    @QtUninvokable
	public final NavigableMap headMap(K toKey, boolean inclusive) {
		QtJambiMapIteratorObject k = lowerBound(toKey);
		NavigableMap map;
		try {
			map = this.getClass().getConstructor().newInstance();
		} catch (Throwable e) {
			map = new TreeMap<>(comparator());
		}
		if(!k.equals(end())) {
			for(QtJambiMapIteratorObject iterator = begin(); !iterator.equals(k); iterator.increment()) {
				map.put(iterator.checkedKey(), iterator.checkedValue());
			}
			if(inclusive) {
				map.put(k.checkedKey(), k.checkedValue());
			}
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
    @QtUninvokable
	public final NavigableMap tailMap(K fromKey, boolean inclusive) {
		QtJambiMapIteratorObject k = lowerBound(fromKey);
		QtJambiMapIteratorObject end = end();
		NavigableMap map;
		try {
			map = this.getClass().getConstructor().newInstance();
		} catch (Throwable e) {
			map = new TreeMap<>(comparator());
		}
		if(!k.equals(end)) {
			if(!inclusive) {
				k.increment();
			}
			for(;!k.equals(end); k.increment()) {
				map.put(k.checkedKey(), k.checkedValue());
			}
		}
		return map;
	}
	
	@Override
    @QtUninvokable
	public final Set> entrySet() {
		Comparator comparator = comparator();
		Set> entrySet = new TreeSet<>((e1, e2)->comparator.compare(e1.getKey(), e2.getKey()));
		for(QPair pair : this) {
			entrySet.add(new AbstractMap.SimpleImmutableEntry<>(pair.first, pair.second));
		}
		return entrySet;
	}
	
	@Override
    @QtUninvokable
	public final Set keySet() {
		TreeSet set = new TreeSet<>(comparator());
		set.addAll(keys());
		return set;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy