Java tutorial
/* * Argus Installer v2 -- A Better School Zip Alternative Copyright (C) 2014 Matthew * Crocco * * This program is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this * program. If not, see <http://www.gnu.org/licenses/>. */ package com.mattc.argus2.io; import java.io.File; import java.util.Enumeration; import java.util.Iterator; import java.util.Locale; import java.util.Map; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; import com.google.common.collect.Iterators; import com.google.common.collect.Maps; import com.mattc.argus2.Ref; import com.mattc.argus2.Settings; import com.mattc.argus2.misc.Pair; import com.mattc.argus2.util.Console; @SuppressWarnings({ "rawtypes", "unchecked" }) public class Configs { private static final Map<String, IConfigurableFile<?>> fileMap = Maps.newHashMap(); public static void applyFunctionTo(String name, Function<Object, Object> func) { Iterators.transform(getIteratorFor(name), func); } public static <T> Enumeration<T> getEnumerationFor(String name) { return getFile(name).getAsEnumeration(); } public static <T> Iterator<T> getIteratorFor(String name) { return getFile(name).getAsIterator(); } public static <T extends IConfigurableFile> T getFile(String name) { Preconditions.checkArgument(fileMap.containsKey(name), "Invalid Name!"); return (T) fileMap.get(name); } public static <T extends IConfigurableFile> void register(String name, T file) { Preconditions.checkArgument(!fileMap.containsKey(name), "That Name is Occupied! Choose a different one... (Name = " + name + ")"); fileMap.put(name, file); Console.debug("Registered " + file.getClass() + " as \"" + name + "\"..."); } public static <T extends IConfigurableFile> Pair<String, T> deregister(String name) { Preconditions.checkArgument(fileMap.containsKey(name), "No File Occupies that Name Slot! (Name = " + name + ")"); final IConfigurableFile<?> f = fileMap.get(name); fileMap.remove(name); Console.debug("De-registered " + name); return new Pair<String, T>(name, (T) f); } private static final DateTimeFormatter dtFormatter = DateTimeFormat .forPattern(Settings.Text.COMMENT_DATE_TIME_FORMAT).withLocale(Locale.US); /** * Format a String of either a single line or multiple lines into <br /> * comment format, similar to the Comment Format used in * {@link java.util.Properties}. <br /> * * @param comment * Unformatted or Formatted Comment Text * @param includeTime * If true, print the current time as a separate comment line. * @return Comment Text formatted properly */ public static String formatComments(String comment, boolean includeTime) { if (includeTime) { comment += String.format("%n%n%s", new DateTime().toString(dtFormatter)); } final Splitter splitter = Splitter.onPattern("\r?\n").trimResults(); final StringBuilder sb = new StringBuilder(); Iterable<String> iterable = splitter.split(comment); iterable = Iterables.transform(iterable, new Function<String, String>() { @Override public String apply(String input) { if (input.startsWith("#")) return input; else if (input.isEmpty()) return "#"; else return "# " + input; } }); for (final String s : iterable) { sb.append(s).append(System.getProperty("line.separator")); } sb.append(System.getProperty("line.separator")); return sb.toString(); } /** * Iterates through list and removes any files deemed non-existant. <br /> * <br /> * Achieves this by using {@link ListFile#applyMutator(Function)} and apply a <br /> * Function<String, String>. * * @param file */ public static final void ensureFilesExist(ListFile file) { file.applyMutator(new Function<String, String>() { private final File archiveDir = Ref.getArchivesDir(); @Override public String apply(String input) { final File f = new File(this.archiveDir, input); if (f.exists()) return input; else return null; } }); } /** * Iterates through list and removes any files deemed non-existant. <br /> * <br /> * Achieves this by using {@link ListFile#applyMutator(Function)} and apply a <br /> * Function<String, String>. * * @param file */ public static final String formatComments(String comments) { return formatComments(comments, false); } protected static void updateFiles() { for (final IConfigurableFile<?> file : fileMap.values()) { file.reload(); } } public final static class Comments { public static final String ARCHIVES = "All archive files should be listed here as a path\nrelative to the 'config' directory.\n\nThis MUST be the full archive name."; public static final String REPLACEMENTS = "Any files that require replacement should be listed here.\nSimply put <relative-path-to-target>=<relative-path-to-source>\nThe Relative Path to Target is the path to the file we need to replace,\nrelative to the '/config' directory.\n\nRelative Path to Source is the path to the file replacing the target.\nRelative to the '/replacer' directory.\n"; public static final String SETTINGS = "Some of these settings may be deprecated which will be documents.\nThese settings should remain teir respective types. i.e. \nonly replace a 'true' or 'false' with a 'true' or 'false'; a number with\n another number."; public static final String DOWNLOADS = null; // TODO } }