Skip to content

Instantly share code, notes, and snippets.

@zeusbaba
Last active December 14, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeusbaba/5107165 to your computer and use it in GitHub Desktop.
Save zeusbaba/5107165 to your computer and use it in GitHub Desktop.
FileIndexerUtil for this & that
/***
* Copyleft - Almighty Matrix!
*
* @author: yg@wareninja.com
* @see https://github.com/WareNinja
* disclaimer: I code for fun, dunno what I'm coding about :)
*/
package utils;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
/***
* FileIndexerUtil parses specific file contents into searchable form
* Exposed interfaces can be used for generic actions against the entries of the parsed file.
*
* @remarks input file content must be in format as:
* foo|bar
* foo2|bar2
* .
* .
*/
public class FileIndexerUtil {
Map<String, String> entryMap = new LinkedHashMap<String, String>();
/**
* just a Constructor for the matrix :D
*/
public FileIndexerUtil() {
}
/**
* Parses the given file into searchable Map form
* @param fileName name of the file to load & parse
*/
public void parseFile(String fileName) {
BufferedReader bReader = null;
entryMap.clear();
try {
bReader = new BufferedReader(new FileReader(fileName));
String line;
String[] lineItems;
while ((line = bReader.readLine()) != null) {
if (line.contains("|")) {
lineItems = line.split("\\|");
entryMap.put(lineItems[0], lineItems[1]);
}
else {
entryMap.put(line, line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns true if the file contains entry for the specified key.
* @param key whose presence is to be searched
* @return true if the file contains entry for the specified key.
*/
public boolean containsKey(String key) {
return key==null? false : entryMap.containsKey(key);
}
/**
* Returns true if the file contains entry for the specified value.
* @param value whose presence is to be searched
* @return true if the file contains entry for the specified value.
*/
public boolean containsValue(String value) {
boolean containsValue = false;
for (Map.Entry<String, String> entry:entryMap.entrySet()) {
if (entry.getValue().equalsIgnoreCase(value)) {
containsValue = true;
break;
}
}
return containsValue;
}
/**
* Returns the value of the entry if the file contains the specified key or null if this file contains no entry for the key.
* @param key whose presence is to be searched
* @return the value of the entry if the file contains the specified key or null if this file contains no entry for the key.
*/
public String getValue(String key) {
if (!containsKey(key)) return null;
return entryMap.get(key);
}
/**
* Removes all entries from memory
*/
public void clear() {
entryMap.clear();
}
/**
* Returns the number of entries
* @return the number of entries
*/
public int size() {
return entryMap.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment