package com.nicewuerfel.blockown.protection;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.nicewuerfel.blockown.Material;
import com.nicewuerfel.blockown.User;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
final class ProtectionLoader {
private static final char SEPARATOR = ';';
private static final String PROTECTION_FILE_NAME = "protection_%s.xml";
@Nonnull
private final File dataFolder;
public ProtectionLoader(@Nonnull File dataFolder) {
this.dataFolder = dataFolder;
}
@Nonnull
private File getLockedFile() {
return new File(dataFolder, String.format(PROTECTION_FILE_NAME, "locked"));
}
@Nonnull
private File getProtectionsFile() {
return new File(dataFolder, String.format(PROTECTION_FILE_NAME, "protections"));
}
@Nonnull
private File getFriendsFile() {
return new File(dataFolder, String.format(PROTECTION_FILE_NAME, "friends"));
}
@Nonnull
Map<User, Set<Material>> loadLocked(int initCapacity) {
Map<User, Set<Material>> result = new ConcurrentHashMap<>(initCapacity);
Properties props = new Properties();
File file = getLockedFile();
if (file.exists()) {
try {
props.loadFromXML(new FileInputStream(file));
} catch (IOException ignored) {
return result;
}
for (Object keyObject : props.keySet()) {
String key = keyObject.toString();
User user = User.getInstance(UUID.fromString(key));
List<String> value = Splitter.on(SEPARATOR).splitToList(props.getProperty(key));
Set<Material> materials =
Collections.newSetFromMap(new ConcurrentHashMap<Material, Boolean>(value.size() * 2));
for (String materialName : value) {
if (!materialName.isEmpty()) {
materials.add(Material.parseMaterial(materialName));
}
}
result.put(user, materials);
}
}
return result;
}
@Nonnull
Map<User, Set<Material>> loadProtections(int initCapacity) {
Map<User, Set<Material>> result = new ConcurrentHashMap<>(initCapacity);
Properties props = new Properties();
File file = getProtectionsFile();
if (file.exists()) {
try {
props.loadFromXML(new FileInputStream(file));
} catch (IOException ignored) {
return result;
}
for (Object keyObject : props.keySet()) {
String key = keyObject.toString();
User user = User.getInstance(UUID.fromString(key));
List<String> value = Splitter.on(SEPARATOR).splitToList(props.getProperty(key));
Set<Material> materials =
Collections.newSetFromMap(new ConcurrentHashMap<Material, Boolean>(value.size() * 2));
for (String materialName : value) {
if (!materialName.isEmpty()) {
materials.add(Material.parseMaterial(materialName));
}
}
result.put(user, materials);
}
}
return result;
}
@Nonnull
Map<User, Set<User>> loadFriends(int initCapacity) {
Map<User, Set<User>> result = new ConcurrentHashMap<>(initCapacity);
Properties props = new Properties();
File file = getFriendsFile();
if (file.exists()) {
try {
props.loadFromXML(new FileInputStream(file));
} catch (IOException ignored) {
return result;
}
for (Object keyObject : props.keySet()) {
String key = keyObject.toString();
User user = User.getInstance(UUID.fromString(key));
List<String> value = Splitter.on(SEPARATOR).splitToList(props.getProperty(key));
Set<User> users =
Collections.newSetFromMap(new ConcurrentHashMap<User, Boolean>(value.size() * 2));
for (String uuidString : value) {
if (!uuidString.isEmpty()) {
users.add(User.getInstance(UUID.fromString(uuidString)));
}
}
result.put(user, users);
}
}
return result;
}
void storeLocked(Map<User, Set<Material>> locks) throws IOException {
Properties props = new Properties();
for (Entry<User, Set<Material>> entry : locks.entrySet()) {
String key = entry.getKey().getUniqueId().toString();
String value = Joiner.on(SEPARATOR).join(entry.getValue());
props.setProperty(key, value);
}
FileOutputStream fos = new FileOutputStream(getLockedFile());
try {
props.storeToXML(fos, null);
} catch (IOException e) {
fos.close();
throw e;
}
fos.close();
}
void storeProtections(Map<User, Set<Material>> protections) throws IOException {
Properties props = new Properties();
for (Entry<User, Set<Material>> entry : protections.entrySet()) {
String key = entry.getKey().getUniqueId().toString();
String value = Joiner.on(SEPARATOR).join(entry.getValue());
props.setProperty(key, value);
}
FileOutputStream fos = new FileOutputStream(getProtectionsFile());
try {
props.storeToXML(fos, null);
} catch (IOException e) {
fos.close();
throw e;
}
fos.close();
}
void storeFriends(Map<User, Set<User>> friends) throws IOException {
Properties props = new Properties();
for (Entry<User, Set<User>> entry : friends.entrySet()) {
String key = entry.getKey().getUniqueId().toString();
String value = Joiner.on(SEPARATOR).join(entry.getValue());
props.setProperty(key, value);
}
FileOutputStream fos = new FileOutputStream(getFriendsFile());
try {
props.storeToXML(fos, null);
} catch (IOException e) {
fos.close();
throw e;
}
fos.close();
}
}