| 1 | package org.jtoolkit.essence.utils; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import org.jetbrains.annotations.Nullable; |
| 5 | import org.jtoolkit.essence.concurrency.Immutable; |
| 6 | |
| 7 | import java.util.*; |
| 8 | |
| 9 | /* |
| 10 | Copyright 2006 Peter Lawrey |
| 11 | |
| 12 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | you may not use this file except in compliance with the License. |
| 14 | You may obtain a copy of the License at |
| 15 | |
| 16 | http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | |
| 18 | Unless required by applicable law or agreed to in writing, software |
| 19 | distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | See the License for the specific language governing permissions and |
| 22 | limitations under the License. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * A class which has a unique name for a given name space. Useful for comments. |
| 27 | * |
| 28 | * @author Peter Lawrey |
| 29 | */ |
| 30 | public interface Named { |
| 31 | /** |
| 32 | * @return A printable name. |
| 33 | */ |
| 34 | @NotNull public String getName(); |
| 35 | |
| 36 | public interface Source<T> extends Named { |
| 37 | Source EMPTY_SOURCE = new MapSource(Collections.emptyMap()); |
| 38 | /** |
| 39 | * @return names available in this source. |
| 40 | */ |
| 41 | @NotNull public Set<String> getNames(); |
| 42 | |
| 43 | /** |
| 44 | * @param name of the object. |
| 45 | * @return the object in source or null. |
| 46 | */ |
| 47 | @Nullable public T getValue(@NotNull String name); |
| 48 | } |
| 49 | |
| 50 | @Immutable |
| 51 | public static class ContextSource<T> implements Source<T> { |
| 52 | private static final String SEP = ".."; |
| 53 | private final String name; |
| 54 | private final Source<T> source; |
| 55 | private final String[] contexts; |
| 56 | |
| 57 | public ContextSource(String name, Source<T> source, String[] contexts) { |
| 58 | this.name = name; |
| 59 | this.source = source; |
| 60 | this.contexts = contexts; |
| 61 | } |
| 62 | |
| 63 | @NotNull public Set<String> getNames() { |
| 64 | Set<String> ret = new LinkedHashSet<String>(); |
| 65 | for(String name: source.getNames()) { |
| 66 | int pos = name.indexOf(SEP); |
| 67 | if (pos>=0) |
| 68 | name = name.substring(pos); |
| 69 | ret.add(name); |
| 70 | } |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | public T getValue(@NotNull String name){ |
| 75 | for(String context:contexts) { |
| 76 | String key = context.length() == 0 ? name : name + SEP + context; |
| 77 | T value = source.getValue(key); |
| 78 | if (value != null) |
| 79 | return value; |
| 80 | } |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | @NotNull public String getName() { |
| 85 | return name; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Immutable |
| 90 | public static class MapSource<T> implements Source<T> { |
| 91 | private final Map<String, T> map; |
| 92 | |
| 93 | public MapSource(@NotNull Map<String, T> map ) { |
| 94 | this.map = map; |
| 95 | } |
| 96 | |
| 97 | @NotNull public Set<String> getNames() { |
| 98 | return map.keySet(); |
| 99 | } |
| 100 | |
| 101 | public T getValue(@NotNull String name) { |
| 102 | return map.get(name); |
| 103 | } |
| 104 | |
| 105 | @NotNull public String getName() { |
| 106 | Object obj = getValue("name"); |
| 107 | if (obj == null) |
| 108 | return toString(); |
| 109 | return obj.toString(); |
| 110 | } |
| 111 | |
| 112 | public boolean equals(Object o) { |
| 113 | if (this == o) return true; |
| 114 | if (o == null || getClass() != o.getClass()) return false; |
| 115 | MapSource mapSource = (MapSource) o; |
| 116 | return map.equals(mapSource.map); |
| 117 | } |
| 118 | |
| 119 | public int hashCode() { |
| 120 | return map.hashCode() * 17; |
| 121 | } |
| 122 | |
| 123 | public String toString() { |
| 124 | return "MapSource "+map; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | @Immutable |
| 130 | public static class PairedSource<T> extends Pair<Source<T>, Source<T>> implements Source<T> { |
| 131 | public PairedSource(Source<T> first, Source<T> second) { |
| 132 | super(first, second); |
| 133 | } |
| 134 | |
| 135 | @NotNull public Set<String> getNames() { |
| 136 | Set<String> ret = new LinkedHashSet<String>(first.getNames()); |
| 137 | ret.addAll(second.getNames()); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | public T getValue(@NotNull String name) throws NoSuchElementException { |
| 142 | try { |
| 143 | return first.getValue(name); |
| 144 | } catch (NoSuchElementException e) { |
| 145 | return second.getValue(name); |
| 146 | } } |
| 147 | |
| 148 | @NotNull public String getName() { |
| 149 | try { |
| 150 | return (String) getValue("name"); |
| 151 | } catch (NoSuchElementException ignored) { |
| 152 | return toString(); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | */ |
| 157 | } |