manifold.tuple.rt.api.Tuple Maven / Gradle / Ivy
/*
* Copyright (c) 2022 - Manifold Systems LLC
*
* 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 manifold.tuple.rt.api;
import manifold.ext.rt.api.Self;
import manifold.util.ManExceptionUtil;
import java.util.*;
/**
* Root interface for tuples.
*
* See {@link manifold.tuple.rt.internal.GeneratedTuple}
*/
public interface Tuple extends Iterable
{
/**
* The tuple's labels, natural order.
*/
List orderedLabels();
/**
* The tuple's values, natural order.
*/
List> orderedValues();
/**
* The tuple's name/value pairs, natural order.
*/
@Override
Iterator iterator();
/**
* Make a map reflecting the name/value pairs of this tuple.
* @param recursive If true, recursively calls this method for values that are tuples.
* @return A map reflecting name/value pairs of this tuple.
*/
default Map toMap( boolean recursive )
{
Map map = new LinkedHashMap<>();
for( TupleItem item : this )
{
String name = item.getName();
Object value = item.getValue();
if( recursive && value instanceof Tuple )
{
value = ((Tuple)value).toMap( true );
}
map.put( name, value );
}
return map;
}
/**
* @return A shallow copy of this tuple. The return type is the receiver's type.
*/
default @Self Tuple copy()
{
try
{
return (Tuple)getClass().getConstructors()[0].newInstance( orderedValues().toArray() );
}
catch( Exception e )
{
throw ManExceptionUtil.unchecked( e );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy