1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.foreign.layout;
  26 
  27 import java.util.Collections;
  28 import java.util.HashMap;
  29 import java.util.LinkedHashMap;
  30 import java.util.Map;
  31 import java.util.stream.Collectors;
  32 
  33 abstract class AbstractDescriptor<L extends AbstractDescriptor<L>> implements Descriptor {
  34     private final Map<String, String> annotations;
  35 
  36     public AbstractDescriptor(Map<String, String> annotations) {
  37         this.annotations = Collections.unmodifiableMap(annotations);
  38     }
  39 
  40     @Override
  41     public Map<String, String> annotations() {
  42         return annotations;
  43     }
  44 
  45     @Override
  46     public L stripAnnotations() {
  47         return withAnnotations(NO_ANNOS);
  48     }
  49 
  50     @Override
  51     public L withAnnotation(String name, String value) {
  52         Map<String, String> newAnnotations = new LinkedHashMap<>(annotations);
  53         newAnnotations.put(name, value);
  54         return withAnnotations(newAnnotations);
  55     }
  56 
  57     @Override
  58     public L withName(String name) {
  59         return withAnnotation(NAME, name);
  60     }
  61 
  62     abstract L withAnnotations(Map<String, String> annotations);
  63 
  64     String wrapWithAnnotations(String s) {
  65         if (!annotations.isEmpty()) {
  66             return String.format("%s%s",
  67                     s, annotations.entrySet().stream()
  68                             .map(e -> !e.getKey().equals(NAME) ?
  69                                     String.format("(%s=%s)", e.getKey(), e.getValue()) :
  70                                     String.format("(%s)", e.getValue()))
  71                             .collect(Collectors.joining()));
  72         } else {
  73             return s;
  74         }
  75     }
  76 
  77     @Override
  78     public int hashCode() {
  79         return annotations.hashCode();
  80     }
  81 
  82     @Override
  83     public boolean equals(Object other) {
  84         if (this == other) {
  85             return true;
  86         }
  87 
  88         if (!(other instanceof AbstractDescriptor)) {
  89             return false;
  90         }
  91 
  92         return annotations.equals(((AbstractDescriptor)other).annotations);
  93     }
  94 
  95     static final Map<String, String> NO_ANNOS = Collections.unmodifiableMap(new HashMap<>());
  96 }