001/* 002 * Copyright 2021 Siroshun09 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.github.siroshun09.mcmessage.loader; 018 019import com.github.siroshun09.mcmessage.message.KeyedMessage; 020import com.github.siroshun09.mcmessage.message.Message; 021import com.github.siroshun09.mcmessage.util.LocaleParser; 022import org.jetbrains.annotations.NotNull; 023import org.jetbrains.annotations.Nullable; 024import org.jetbrains.annotations.Unmodifiable; 025 026import java.nio.file.Files; 027import java.nio.file.Path; 028import java.text.MessageFormat; 029import java.util.HashMap; 030import java.util.Locale; 031import java.util.Map; 032import java.util.Set; 033import java.util.stream.Collectors; 034 035public abstract class AbstractMessageLoader implements MessageLoader { 036 037 private final Path path; 038 private final Map<String, String> messageMap; 039 040 protected AbstractMessageLoader(Path path) { 041 this.path = path; 042 messageMap = new HashMap<>(); 043 } 044 045 @Override 046 public boolean existsFile() { 047 return Files.isRegularFile(path); 048 } 049 050 @Override 051 public @Nullable Message getMessage(@NotNull String key) { 052 var message = messageMap.get(key); 053 return message != null ? Message.create(message) : null; 054 } 055 056 @Override 057 public @NotNull @Unmodifiable Set<KeyedMessage> getMessages() { 058 return messageMap.entrySet() 059 .stream() 060 .map(e -> KeyedMessage.create(e.getKey(), e.getValue())) 061 .collect(Collectors.toUnmodifiableSet()); 062 } 063 064 @Override 065 public @Nullable Locale getLocale() { 066 return parseLocaleFromFileName(); 067 } 068 069 @Override 070 public @NotNull Map<String, MessageFormat> toMessageFormatMap() { 071 return messageMap.entrySet() 072 .stream() 073 .collect( 074 Collectors.toUnmodifiableMap( 075 Map.Entry::getKey, 076 e -> new MessageFormat(e.getValue()) 077 ) 078 ); 079 } 080 081 protected @NotNull Path getPath() { 082 return path; 083 } 084 085 protected @NotNull Map<String, String> getMessageMap() { 086 return messageMap; 087 } 088 089 private @Nullable Locale parseLocaleFromFileName() { 090 var filePath = path.getFileName(); 091 092 if (filePath == null) { 093 return null; 094 } 095 096 var fileName = filePath.toString().toCharArray(); 097 var builder = new StringBuilder(); 098 099 for (var c : fileName) { 100 if (c != '.') { 101 builder.append(c); 102 } else { 103 break; 104 } 105 } 106 107 return LocaleParser.parse(builder.toString()); 108 } 109}