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

com.rapiddweller.common.file.FileHistory Maven / Gradle / Ivy

Go to download

'rapiddweller Common' is an open source Java library forked from Databene Commons by Volker Bergmann. It provides extensions to the Java core library by utility classes, abstract concepts and concrete implementations.

There is a newer version: 2.0.1-jdk-11
Show newest version
/*
 * Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
 * All rights reserved.
 *
 * 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.rapiddweller.common.file;

import com.rapiddweller.common.Assert;
import com.rapiddweller.common.CollectionUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 * Provides a file history using the Preferences API.

* Created: 27.03.2016 09:02:58 * * @author Volker Bergmann * @since 1.0.8 */ public class FileHistory { /** * The constant HISTORY_LENGTH_LIMIT. */ public static final int HISTORY_LENGTH_LIMIT = 50; private static final String RECENT_FILE_PREFIX = "recent_file_"; private final Class clazz; private final ArrayDeque files; private final boolean toleratingFailure; /** * Instantiates a new File history. * * @param clazz the clazz * @param length the length * @param toleratingFailure the tolerating failure */ public FileHistory(Class clazz, int length, boolean toleratingFailure) { this.clazz = clazz; Assert.lessOrEqual(length, HISTORY_LENGTH_LIMIT, "length"); this.files = new ArrayDeque<>(length); this.toleratingFailure = toleratingFailure; load(); } /** * Get files file [ ]. * * @return the file [ ] */ public File[] getFiles() { return CollectionUtil.toArray(files, File.class); } /** * Gets most recent folder. * * @param defaultFolder the default folder * @return the most recent folder */ public File getMostRecentFolder(File defaultFolder) { if (files.isEmpty()) { return defaultFolder; } else { return files.getLast().getParentFile(); } } /** * Add file and save. * * @param file the file */ public void addFileAndSave(File file) { addFile(file); save(); } /** * Add file. * * @param file the file */ public void addFile(File file) { try { file = normalize(file); remove(file); files.addFirst(file); } catch (IOException e) { if (!toleratingFailure) { throw new RuntimeException("Failed to update file history", e); } } } /** * Load. */ public void load() { Preferences node = Preferences.userNodeForPackage(clazz); for (int i = 0; i < HISTORY_LENGTH_LIMIT; i++) { String path = node.get(RECENT_FILE_PREFIX + i, null); if (path != null) { File file = new File(path); appendFile(file); } } } /** * Save. */ public void save() { Preferences node = Preferences.userNodeForPackage(clazz); Iterator iterator = files.iterator(); for (int i = 0; iterator.hasNext(); i++) { node.put(RECENT_FILE_PREFIX + i, iterator.next().getAbsolutePath()); } try { node.flush(); } catch (BackingStoreException e) { if (!toleratingFailure) { throw new RuntimeException("Failed to save file history", e); } } } // private helpers ------------------------------------------------------------------------------------------------- private static File normalize(File file) throws IOException { return file.getCanonicalFile(); } private void remove(File file) { files.removeIf(file::equals); } private void appendFile(File file) { try { file = normalize(file); remove(file); files.addLast(file); } catch (IOException e) { if (!toleratingFailure) { throw new RuntimeException("Failed to update file history", e); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy