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

com.lithium.flow.filer.SortedFiler Maven / Gradle / Ivy

/*
 * Copyright 2015 Lithium Technologies, Inc.
 *
 * 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.lithium.flow.filer;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Nonnull;

/**
 * Decorates an instance of {@link Filer} to sort file details by name in ascending or descending order.
 * 
 * @author Matt Ayres
 */
public class SortedFiler extends DecoratedFiler {
	public static final Comparator NAME_ASC = (d1, d2) -> d1.getName().compareTo(d2.getName());

	public static final Comparator NAME_DESC = (d1, d2) -> d2.getName().compareTo(d1.getName());

	private final Comparator comparator;

	public SortedFiler(@Nonnull Filer delegate) {
		this(delegate, NAME_ASC);
	}

	public SortedFiler(@Nonnull Filer delegate, @Nonnull Comparator comparator) {
		super(checkNotNull(delegate));
		this.comparator = checkNotNull(comparator);
	}

	@Override
	@Nonnull
	public List listRecords(@Nonnull String path) throws IOException {
		List records = super.listRecords(path);
		Collections.sort(records, comparator);
		return records;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy