com.github.tommyettinger.ds.interop.ConversionToGDX Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jdkgdxds_interop Show documentation
Show all versions of jdkgdxds_interop Show documentation
Inter-operate between libGDX and jdkgdxds.
The newest version!
/*
* Copyright (c) 2023 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.tommyettinger.ds.interop;
import com.badlogic.gdx.utils.IntFloatMap;
import com.badlogic.gdx.utils.IntIntMap;
import com.badlogic.gdx.utils.IntSet;
import com.badlogic.gdx.utils.ObjectFloatMap;
import com.badlogic.gdx.utils.ObjectIntMap;
import com.badlogic.gdx.utils.ObjectLongMap;
import com.badlogic.gdx.utils.ObjectSet;
import com.badlogic.gdx.utils.*;
import com.github.tommyettinger.ds.*;
import com.github.tommyettinger.ds.support.util.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* Converts from arbitrary data structures that implement JDK interfaces to libGDX data structures (which typically
* implement {@link Iterable} at most). You can send JDK classes like {@link java.util.ArrayList} to methods in this to
* convert to {@link Array} or {@link ObjectSet}, just as you can send jdkgdxds classes like {@link ObjectList}.
*/
public class ConversionToGDX {
/**
* Can be used to convert from any Collection to a new Array of the same element type.
* This can take an {@link ObjectList}, {@link com.github.tommyettinger.ds.ObjectSet},
* {@link NumberedSet}, or any JDK Collection, to name a few.
* @param from a Collection from the JDK, jdkgdxds, or some other library
* @param the element type for {@code from} and the result
* @return a new Array of type T holding the items of {@code from} (this does not give a Class to the Array constructor)
*/
public static Array toArray(Collection extends T> from) {
Array array = new Array<>(from.size());
for(T t : from)
array.add(t);
return array;
}
/**
* Can be used to convert from any jdkgdxds int-based collection (such as {@link IntList},
* {@link com.github.tommyettinger.ds.IntSet}, or {@link IntObjectMap.Keys}) to a libGDX IntArray.
* @param from a primitive-int-backed data structure from jdkgdxds
* @return a new IntArray holding the items of {@code from}
*/
public static IntArray toIntArray(PrimitiveCollection.OfInt from) {
IntArray array = new IntArray(from.size());
IntIterator it = from.iterator();
while (it.hasNext()){
array.add(it.nextInt());
}
return array;
}
/**
* Can be used to convert from any jdkgdxds long-based collection (such as {@link LongList},
* {@link LongSet}, or {@link LongObjectMap.Keys}) to a libGDX LongArray.
* @param from a primitive-long-backed data structure from jdkgdxds
* @return a new LongArray holding the items of {@code from}
*/
public static LongArray toLongArray(PrimitiveCollection.OfLong from) {
LongArray array = new LongArray(from.size());
LongIterator it = from.iterator();
while (it.hasNext()){
array.add(it.nextLong());
}
return array;
}
/**
* Can be used to convert from any jdkgdxds float-based collection (such as {@link FloatList}
* or {@link com.github.tommyettinger.ds.ObjectFloatMap.Values}) to a libGDX FloatArray.
* @param from a primitive-float-backed data structure from jdkgdxds
* @return a new FloatArray holding the items of {@code from}
*/
public static FloatArray toFloatArray(PrimitiveCollection.OfFloat from) {
FloatArray array = new FloatArray(from.size());
FloatIterator it = from.iterator();
while (it.hasNext()){
array.add(it.nextFloat());
}
return array;
}
/**
* Can be used to convert from any jdkgdxds byte-based collection (such as {@link ByteList})
* to a libGDX ByteArray.
* @param from a primitive-float-backed data structure from jdkgdxds
* @return a new ByteArray holding the items of {@code from}
*/
public static ByteArray toByteArray(PrimitiveCollection.OfByte from) {
ByteArray array = new ByteArray(from.size());
ByteIterator it = from.iterator();
while (it.hasNext()){
array.add(it.nextByte());
}
return array;
}
/**
* Can be used to convert from any jdkgdxds char-based collection (such as {@link CharList})
* to a libGDX CharArray.
* @param from a primitive-float-backed data structure from jdkgdxds
* @return a new CharArray holding the items of {@code from}
*/
public static CharArray toCharArray(PrimitiveCollection.OfChar from) {
CharArray array = new CharArray(from.size());
CharIterator it = from.iterator();
while (it.hasNext()){
array.add(it.nextChar());
}
return array;
}
/**
* Can be used to convert from any jdkgdxds short-based collection (such as {@link ShortList})
* to a libGDX ShortArray.
* @param from a primitive-float-backed data structure from jdkgdxds
* @return a new ShortArray holding the items of {@code from}
*/
public static ShortArray toShortArray(PrimitiveCollection.OfShort from) {
ShortArray array = new ShortArray(from.size());
ShortIterator it = from.iterator();
while (it.hasNext()){
array.add(it.nextShort());
}
return array;
}
/**
* Can be used to convert from any jdkgdxds boolean-based collection (such as {@link BooleanList})
* to a libGDX BooleanArray.
* @param from a primitive-float-backed data structure from jdkgdxds
* @return a new BooleanArray holding the items of {@code from}
*/
public static BooleanArray toBooleanArray(PrimitiveCollection.OfBoolean from) {
BooleanArray array = new BooleanArray(from.size());
BooleanIterator it = from.iterator();
while (it.hasNext()){
array.add(it.nextBoolean());
}
return array;
}
// there is no DoubleArray in libGDX... for some reason.
/**
* Can be used to convert from any JDK Map to a new libGDX ArrayMap with the same key and value types.
* Note that jdkgdxds does not have a direct equivalent to libGDX's ArrayMap, because ArrayMap is only the "right
* choice" in some extremely narrow situations, and is only barely used inside libGDX (and only in its 3D code).
* @param from a Map from the JDK, jdkgdxds, or some other library
* @param the key type
* @param the value type
* @return a new ArrayMap holding the keys and values of {@code from} (this does not give a Class to the ArrayMap constructor)
*/
public static ArrayMap toArrayMap(Map extends K, ? extends V> from) {
ArrayMap arrayMap = new ArrayMap<>(true, from.size());
for(Map.Entry extends K, ? extends V> e : from.entrySet())
arrayMap.put(e.getKey(), e.getValue());
return arrayMap;
}
/**
* Can be used to convert from any JDK Collection, such as a jdkgdxds ObjectSet or ObjectList, or
* a JDK ArrayList, to a new libGDX {@link ObjectSet}, using only the unique items in {@code from}.
* @param from anything that implements the JDK Collection interface
* @return a new libGDX ObjectSet holding the unique items in {@code from}
*/
public static ObjectSet toObjectSet(Collection extends T> from) {
ObjectSet set = new ObjectSet<>(from.size());
Iterator extends T> it = from.iterator();
while (it.hasNext()) {
set.add(it.next());
}
return set;
}
/**
* Can be used to convert from any JDK Collection, such as a jdkgdxds ObjectOrderedSet or ObjectList, or
* a JDK ArrayList, to a new libGDX {@link OrderedSet}, using only the unique items in {@code from}.
* @param from anything that implements the JDK Collection interface
* @return a new libGDX OrderedSet holding the unique items in {@code from}
*/
public static OrderedSet toOrderedSet(Collection extends T> from) {
OrderedSet set = new OrderedSet<>(from.size());
Iterator extends T> it = from.iterator();
while (it.hasNext()) {
set.add(it.next());
}
return set;
}
/**
* Can be used to convert from any int-based PrimitiveCollection, such as an {@link IntList},
* {@link com.github.tommyettinger.ds.IntSet}, or {@link com.github.tommyettinger.ds.IntIntMap.Keys},
* to a new libGDX IntSet, using only the unique items in {@code from}.
* @param from a primitive-int-backed data structure from jdkgdxds
* @return a new IntSet holding the unique items in {@code from}
*/
public static IntSet toIntSet(PrimitiveCollection.OfInt from) {
IntSet set = new IntSet(from.size());
IntIterator it = from.iterator();
while (it.hasNext()) {
set.add(it.nextInt());
}
return set;
}
/**
* Can be used to convert from any JDK Map, such as a {@link java.util.HashMap}, {@link ObjectObjectMap},
* or {@link CaseInsensitiveOrderedMap}, to a libGDX ObjectMap.
* @param from any Map that has a usable {@link Map#keySet()} method
* @param the type of keys; the same in {@code from} and the returned ObjectMap
* @param the type of values; the same in {@code from} and the returned ObjectMap
* @return a new ObjectMap holding all the key-value pairs in {@code from}
*/
public static ObjectMap toObjectMap(Map extends K, ? extends V> from){
ObjectMap map = new ObjectMap<>(from.size());
for(K k : from.keySet()) {
map.put(k, from.get(k));
}
return map;
}
/**
* Can be used to convert from any JDK Map, such as a {@link java.util.LinkedHashMap},
* {@link ObjectObjectOrderedMap}, or {@link CaseInsensitiveMap}, to a libGDX OrderedMap.
* If {@code from} maintains the order of its keys, then the returned OrderedMap will have
* the same order.
* @param from any Map that has a usable {@link Map#keySet()} method
* @param the type of keys; the same in {@code from} and the returned OrderedMap
* @param the type of values; the same in {@code from} and the returned OrderedMap
* @return a new OrderedMap holding all the key-value pairs in {@code from}
*/
public static OrderedMap toOrderedMap(Map extends K, ? extends V> from){
OrderedMap map = new OrderedMap<>(from.size());
for(K k : from.keySet()) {
map.put(k, from.get(k));
}
return map;
}
/**
* Can be used to convert from a jdkgdxds {@link com.github.tommyettinger.ds.ObjectIntMap} or
* {@link ObjectIntOrderedMap} to a libGDX ObjectIntMap.
* @param from a jdkgdxds ObjectIntMap or ObjectIntOrderedMap
* @param the type of keys; the same in {@code from} and the returned ObjectIntMap
* @return a new libGDX ObjectIntMap holding all the key-value pairs in {@code from}
*/
public static ObjectIntMap toObjectIntMap(com.github.tommyettinger.ds.ObjectIntMap extends K> from){
ObjectIntMap map = new ObjectIntMap<>(from.size());
for(K k : from.keySet()) {
map.put(k, from.get(k));
}
return map;
}
/**
* Can be used to convert from a jdkgdxds {@link com.github.tommyettinger.ds.ObjectLongMap} or
* {@link ObjectLongOrderedMap} to a libGDX ObjectLongMap.
* @param from a jdkgdxds ObjectLongMap or ObjectLongOrderedMap
* @param the type of keys; the same in {@code from} and the returned ObjectLongMap
* @return a new libGDX ObjectLongMap holding all the key-value pairs in {@code from}
*/
public static ObjectLongMap toObjectLongMap(com.github.tommyettinger.ds.ObjectLongMap extends K> from){
ObjectLongMap map = new ObjectLongMap<>(from.size());
for(K k : from.keySet()) {
map.put(k, from.get(k));
}
return map;
}
/**
* Can be used to convert from a jdkgdxds {@link com.github.tommyettinger.ds.ObjectFloatMap} or
* {@link ObjectFloatOrderedMap} to a libGDX ObjectFloatMap.
* @param from a jdkgdxds ObjectFloatMap or ObjectFloatOrderedMap
* @param the type of keys; the same in {@code from} and the returned ObjectFloatMap
* @return a new libGDX ObjectFloatMap holding all the key-value pairs in {@code from}
*/
public static ObjectFloatMap toObjectFloatMap(com.github.tommyettinger.ds.ObjectFloatMap extends K> from){
ObjectFloatMap map = new ObjectFloatMap<>(from.size());
for(K k : from.keySet()) {
map.put(k, from.get(k));
}
return map;
}
/**
* Can be used to convert from a jdkgdxds {@link com.github.tommyettinger.ds.IntObjectMap} or
* {@link IntObjectOrderedMap} to a libGDX IntMap.
* @param from a jdkgdxds IntObjectMap or IntObjectOrderedMap
* @param the type of values; the same in {@code from} and the returned IntMap
* @return a new libGDX IntMap holding all the key-value pairs in {@code from}
*/
public static IntMap toIntMap(com.github.tommyettinger.ds.IntObjectMap extends V> from){
IntMap map = new IntMap<>(from.size());
IntIterator it = from.keySet().iterator();
while (it.hasNext()) {
int k = it.nextInt();
map.put(k, from.get(k));
}
return map;
}
/**
* Can be used to convert from a jdkgdxds {@link com.github.tommyettinger.ds.IntIntMap} or
* {@link IntIntOrderedMap} to a libGDX IntIntMap.
* @param from a jdkgdxds IntIntMap or IntIntOrderedMap
* @return a new libGDX IntIntMap holding all the key-value pairs in {@code from}
*/
public static IntIntMap toIntIntMap(com.github.tommyettinger.ds.IntIntMap from){
IntIntMap map = new IntIntMap(from.size());
IntIterator it = from.keySet().iterator();
while (it.hasNext()) {
int k = it.nextInt();
map.put(k, from.get(k));
}
return map;
}
/**
* Can be used to convert from a jdkgdxds {@link com.github.tommyettinger.ds.IntFloatMap} or
* {@link IntFloatOrderedMap} to a libGDX IntFloatMap.
* @param from a jdkgdxds IntFloatMap or IntFloatOrderedMap
* @return a new libGDX IntFloatMap holding all the key-value pairs in {@code from}
*/
public static IntFloatMap toIntFloatMap(com.github.tommyettinger.ds.IntFloatMap from){
IntFloatMap map = new IntFloatMap(from.size());
IntIterator it = from.keySet().iterator();
while (it.hasNext()) {
int k = it.nextInt();
map.put(k, from.get(k));
}
return map;
}
/**
* Can be used to convert from a jdkgdxds {@link com.github.tommyettinger.ds.LongObjectMap} or
* {@link LongObjectOrderedMap} to a libGDX IntMap.
* @param from a jdkgdxds LongObjectMap or LongObjectOrderedMap
* @param the type of values; the same in {@code from} and the returned IntMap
* @return a new libGDX IntMap holding all the key-value pairs in {@code from}
*/
public static LongMap toLongMap(com.github.tommyettinger.ds.LongObjectMap extends V> from){
LongMap map = new LongMap<>(from.size());
LongIterator it = from.keySet().iterator();
while (it.hasNext()) {
long k = it.nextLong();
map.put(k, from.get(k));
}
return map;
}
/**
* Can be used to convert from any JDK Map, such as a {@link java.util.HashMap}, {@link IdentityObjectMap},
* or {@link CaseInsensitiveOrderedMap}, to a libGDX IdentityMap. In almost all cases, IdentityMap is more
* permissive about allowing duplicate keys, so converting to an IdentityMap should be lossless.
* @param from any Map that has a usable {@link Map#keySet()} method
* @param the type of keys; the same in {@code from} and the returned IdentityMap
* @param the type of values; the same in {@code from} and the returned IdentityMap
* @return a new IdentityMap holding all the key-value pairs in {@code from}
*/
public static IdentityMap toIdentityMap(Map extends K, ? extends V> from){
IdentityMap map = new IdentityMap<>(from.size());
for(K k : from.keySet()) {
map.put(k, from.get(k));
}
return map;
}
/**
* Can be used to convert from any Collection to a new libGDX Queue of the same element type.
* This can take an {@link ObjectList}, {@link com.github.tommyettinger.ds.ObjectSet},
* {@link NumberedSet}, or any JDK Collection, to name a few.
*
* Note that Queue in libGDX corresponds to ObjectDeque in jdkgdxds. Both are double-ended queues.
* @param from a Collection from the JDK, jdkgdxds, or some other library
* @param the element type for {@code from} and the result
* @return a new Queue of type T holding the items of {@code from} (this does not give a Class to the Queue constructor)
*/
public static Queue toQueue(Collection extends T> from) {
Queue queue = new Queue<>(from.size());
for(T t : from)
queue.addLast(t);
return queue;
}
/**
* Can be used to convert from any jdkgdxds long-based collection (such as {@link LongList},
* {@link LongSet}, or especially a {@link LongDeque}) to a libGDX LongQueue.
*
* Note that LongQueue in libGDX corresponds to LongDeque in jdkgdxds. Both are double-ended queues.
* @param from a primitive-long-backed data structure from jdkgdxds
* @return a new LongQueue holding the items of {@code from}
*/
public static LongQueue toLongQueue(PrimitiveCollection.OfLong from) {
LongQueue queue = new LongQueue(from.size());
LongIterator it = from.iterator();
while (it.hasNext()){
queue.addLast(it.nextLong());
}
return queue;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy