/* * [Autocorrects.java] * * Summary: Base class for MS Word and Open Office autocorrects. * * Copyright: (c) 2010-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.7+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2010-03-03 initial version */ package com.mindprod.autocorrect; import com.mindprod.common17.EIO; import java.io.File; import java.io.IOException; import java.util.Map; import java.util.TreeMap; import static java.lang.System.out; /** * Base class for MS Word and Open Office autocorrects. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2010-03-03 initial version * @since 2010-03-03 */ abstract class Autocorrects { /** * Map of abbreviations to expansions. No initial size estimate needed */ final TreeMap map = new TreeMap<>(); /** * File that contains these autocorrects */ File backingFile; /** * @param abbreviation for some autocorrect, usually a dummy marker * @param expansion for some autocorrect, usually a dummy marker * * @return true if abbreviation-expansion pair exists in this collection of Autocorrects. */ boolean contains( String abbreviation, String expansion ) { return expansion.equals( map.get( abbreviation ) ); } /** * dumps out abbreviation-expansion pairs, an alphabetic order by abbreviation, debugging tool */ void dump() { out.println( EIO.getCanOrAbsPath( backingFile ) ); int count = 0; for ( Map.Entry e : map.entrySet() ) { final String abbreviation = e.getKey(); final String expansion = e.getValue(); out.println( ++count + " " + abbreviation + " --> " + expansion ); } } /** * Extract all abbreviation-expansion pairs usually so they can be merged into some other Autocorrects collection. * * @return Map of all the abbreviation-expansion pairs. */ Map getAll() { return map; } /** * read a set of autocorrects * * @param source OS filename of containing the autocorrects in some format. * * @throws java.io.IOException if cannot load autocorrects. */ abstract void load( File source ) throws IOException; /** * Merge in additional abbreviation-expansion Pairs overriding existing expansion for the abbreviation. * * @param m Map of abbreviation-expansion Pairs to merge into this Collection. */ void merge( Map m ) { map.putAll( m ); } /** * write a set of possibly modified autocorrects back where they were loaded from. * * @throws java.io.IOException if have trouble saving data */ abstract void save() throws IOException; }