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

com.google.code.liquidform.OrderByClause Maven / Gradle / Ivy

Go to download

Provides a Java Domain Specific Language for building type-safe and refactoring proof JPA queries.

The newest version!
/*
 * Copyright (C) 2008-2009 the original author or authors
 * 
 * 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.google.code.liquidform;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.code.liquidform.internal.AliasFactory;
import com.google.code.liquidform.internal.Pair;

/**
 * Represents the optional ORDER BY part of a query. One an instance of
 * this class has been created using eg
 * {@link FromClause#orderBy(Object)}, additional sort parameters may be added
 * using the {@link #andThenBy(Object)} construct.
 */
public class OrderByClause extends AbstractClause {

	// Can't use LinkedHashMap because it uses equals() on
	// keys, we need ==
	private List> sortInstructions = new ArrayList>();

	/* default */OrderByClause(AbstractClause previousClause,
			Object firstInstruction, SortDirection firstDirection) {
		super(previousClause);
		sortInstructions.add(new Pair(firstInstruction,
				firstDirection, false));
	}

	public OrderByClause andThenBy(Number o, SortDirection dir) {
		sortInstructions.add(new Pair(o, dir, false));
		return this;
	}

	public OrderByClause andThenBy(Number o) {
		return andThenBy(o, SortDirection.ASC);
	}

	public OrderByClause andThenBy(Date o, SortDirection dir) {
		sortInstructions.add(new Pair(o, dir, false));
		return this;
	}

	public OrderByClause andThenBy(Date o) {
		return andThenBy(o, SortDirection.ASC);
	}

	public OrderByClause andThenBy(String o, SortDirection dir) {
		sortInstructions.add(new Pair(o, dir, false));
		return this;
	}

	public OrderByClause andThenBy(String o) {
		return andThenBy(o, SortDirection.ASC);
	}

	public OrderByClause andThenBy(Character o, SortDirection dir) {
		sortInstructions.add(new Pair(o, dir, false));
		return this;
	}

	public OrderByClause andThenBy(Character o) {
		return andThenBy(o, SortDirection.ASC);
	}

	// public OrderByClause andThenBy(Enum o, SortDirection dir) {
	// sortInstructions.add(new Pair(o, dir, false));
	// return this;
	// }
	//
	// public OrderByClause andThenBy(Enum o) {
	// return andThenBy(o, SortDirection.ASC);
	// }

	@Override
	StringBuilder addTo(StringBuilder buffer) {
		buffer.append(" order by ");

		int i = 0;
		for (Pair inst : sortInstructions) {
			buffer.append(AliasFactory.about(inst.first)
					.getTargetRepresentation());
			buffer.append(" ").append(inst.second.stringForm);
			if (++i < sortInstructions.size()) {
				buffer.append(", ");
			}
		}
		return buffer;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy