|
| 1 | +/* This file is part of KeY - https://key-project.org |
| 2 | + * KeY is licensed under the GNU General Public License Version 2 |
| 3 | + * SPDX-License-Identifier: GPL-2.0-only */ |
| 4 | +package de.uka.ilkd.key.logic; |
| 5 | + |
| 6 | +import java.util.Map; |
| 7 | +import java.util.TreeMap; |
| 8 | + |
| 9 | +import org.key_project.logic.HasMeta; |
| 10 | + |
| 11 | +import org.jspecify.annotations.NullMarked; |
| 12 | +import org.jspecify.annotations.Nullable; |
| 13 | + |
| 14 | +/// MetaSpace is a namespace for storing additional information |
| 15 | +/// |
| 16 | +/// @author weigl |
| 17 | +@NullMarked |
| 18 | +public class MetaSpace { |
| 19 | + public static final String SPACE_PREFIX_DOC = "doc/"; |
| 20 | + public static final String SPACE_PREFIX_ORIGIN = "origin/"; |
| 21 | + |
| 22 | + private @Nullable MetaSpace parent; |
| 23 | + private final Map<String, Object> metadata = new TreeMap<>(); |
| 24 | + |
| 25 | + public MetaSpace() { |
| 26 | + } |
| 27 | + |
| 28 | + public MetaSpace(Map<String, Object> documentation) { |
| 29 | + this.metadata.putAll(documentation); |
| 30 | + } |
| 31 | + |
| 32 | + public MetaSpace(MetaSpace parent) { |
| 33 | + this.parent = parent; |
| 34 | + } |
| 35 | + |
| 36 | + private @Nullable Object findMetadata(String key) { |
| 37 | + return metadata.get(key); |
| 38 | + } |
| 39 | + |
| 40 | + public @Nullable String findDocumentation(HasMeta entity) { |
| 41 | + if (entity.getDocumentation() != null) { |
| 42 | + return entity.getDocumentation(); |
| 43 | + } |
| 44 | + return (String) findMetadata(SPACE_PREFIX_DOC + entity.getMetaKey()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns a human-readable source of this term. For example the filename with line and offset. |
| 49 | + */ |
| 50 | + public @Nullable String findOrigin(HasMeta entity) { |
| 51 | + return (String) findMetadata(SPACE_PREFIX_ORIGIN + entity.getMetaKey()); |
| 52 | + } |
| 53 | + |
| 54 | + public void add(MetaSpace space) { |
| 55 | + this.metadata.putAll(space.metadata); |
| 56 | + } |
| 57 | + |
| 58 | + public @Nullable MetaSpace parent() { |
| 59 | + return parent; |
| 60 | + } |
| 61 | + |
| 62 | + public void setDocumentation(HasMeta entity, @Nullable String doc) { |
| 63 | + setMetadata(SPACE_PREFIX_DOC, entity, doc); |
| 64 | + } |
| 65 | + |
| 66 | + public void setOrigin(HasMeta entity, @Nullable String origin) { |
| 67 | + setMetadata(SPACE_PREFIX_ORIGIN, entity, origin); |
| 68 | + } |
| 69 | + |
| 70 | + private void setMetadata(String prefix, HasMeta entity, @Nullable Object val) { |
| 71 | + var key = prefix + entity.getMetaKey(); |
| 72 | + if (val == null) { |
| 73 | + metadata.remove(key); |
| 74 | + } else { |
| 75 | + metadata.put(key, val); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + public MetaSpace copy() { |
| 81 | + return new MetaSpace(metadata); |
| 82 | + } |
| 83 | +} |
0 commit comments