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

com.enonic.xp.query.expr.QueryExpr Maven / Gradle / Ivy

There is a newer version: 7.14.4
Show newest version
package com.enonic.xp.query.expr;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableList;

import com.enonic.xp.annotation.PublicApi;

@PublicApi
public final class QueryExpr
    implements Expression
{
    private final ConstraintExpr constraint;

    private final ImmutableList orderList;

    public QueryExpr( final ConstraintExpr constraint, final Iterable orderSet )
    {
        this.constraint = constraint;

        if ( orderSet != null )
        {
            this.orderList = ImmutableList.copyOf( orderSet );
        }
        else
        {
            this.orderList = ImmutableList.of();
        }
    }

    public QueryExpr( final Iterable orderSet )
    {
        this.constraint = null;

        if ( orderSet != null )
        {
            this.orderList = ImmutableList.copyOf( orderSet );
        }
        else
        {
            this.orderList = ImmutableList.of();
        }
    }


    public static QueryExpr from( final ConstraintExpr constraint, final OrderExpr... orderSet )
    {
        return new QueryExpr( constraint, Arrays.asList( orderSet ) );
    }

    public static QueryExpr from( final ConstraintExpr constraint, final Iterable orderSet )
    {
        return new QueryExpr( constraint, orderSet );
    }

    public ConstraintExpr getConstraint()
    {
        return this.constraint;
    }

    public List getOrderList()
    {
        return this.orderList;
    }

    @Override
    public String toString()
    {
        final StringBuilder str = new StringBuilder();

        if ( this.constraint != null )
        {
            str.append( this.constraint );
        }

        if ( !this.orderList.isEmpty() )
        {
            if ( str.length() > 0 )
            {
                str.append( " " );
            }

            str.append( "ORDER BY " );
            str.append( this.orderList.stream().map( Object::toString ).collect( Collectors.joining( ", " ) ) );
        }

        return str.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy