获取yaml属性 可通过 "." 循环调用 例如这样调用: YamlReader.getInstance().getValueByKey("a.b.c.d") @param key @return
(String key)
| 44 | * @return |
| 45 | */ |
| 46 | public Object getValueByKey(String key) { |
| 47 | String separator = "."; |
| 48 | String[] separatorKeys = null; |
| 49 | if (key.contains(separator)) { |
| 50 | separatorKeys = key.split("\\."); |
| 51 | } else { |
| 52 | return properties.get(key); |
| 53 | } |
| 54 | Map<String, Map<String, Object>> finalValue = new HashMap<>(); |
| 55 | for (int i = 0; i < separatorKeys.length - 1; i++) { |
| 56 | if (i == 0) { |
| 57 | finalValue = (Map) properties.get(separatorKeys[i]); |
| 58 | continue; |
| 59 | } |
| 60 | if (finalValue == null) { |
| 61 | break; |
| 62 | } |
| 63 | finalValue = (Map) finalValue.get(separatorKeys[i]); |
| 64 | } |
| 65 | return finalValue == null ? null : finalValue.get(separatorKeys[separatorKeys.length - 1]); |
| 66 | } |
| 67 | |
| 68 | public String getString(String key) { |
| 69 | return String.valueOf(this.getValueByKey(key)); |
no outgoing calls
no test coverage detected