1 /*
   2  * Copyright (c) 2016, 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 jdk.nashorn.api.tree;
  26 
  27 import java.util.List;
  28 import java.util.stream.Collectors;
  29 import jdk.nashorn.internal.ir.FunctionNode;
  30 import jdk.nashorn.internal.ir.IdentNode;
  31 import jdk.nashorn.internal.ir.Module;
  32 import static jdk.nashorn.api.tree.ModuleTreeImpl.identOrNull;
  33 
  34 final class ImportEntryTreeImpl extends TreeImpl implements ImportEntryTree {
  35     private final long startPos, endPos;
  36     private final IdentifierTree moduleRequest;
  37     private final IdentifierTree importName;
  38     private final IdentifierTree localName;
  39 
  40     private ImportEntryTreeImpl(final long startPos, final long endPos,
  41             final IdentifierTree moduleRequest,
  42             final IdentifierTree importName,
  43             final IdentifierTree localName) {
  44         super(null); // No underlying Node!
  45         this.startPos = startPos;
  46         this.endPos = endPos;
  47         this.moduleRequest = moduleRequest;
  48         this.importName = importName;
  49         this.localName = localName;
  50     }
  51 
  52     private static ImportEntryTreeImpl createImportEntry(final Module.ImportEntry entry) {
  53         return new ImportEntryTreeImpl(entry.getStartPosition(),
  54                 entry.getEndPosition(),
  55                 identOrNull(entry.getModuleRequest()),
  56                 identOrNull(entry.getImportName()),
  57                 identOrNull(entry.getLocalName()));
  58     }
  59 
  60     static List<ImportEntryTreeImpl> createImportList(final List<Module.ImportEntry> importList) {
  61         return importList.stream().
  62             map(ImportEntryTreeImpl::createImportEntry).
  63             collect(Collectors.toList());
  64     }
  65 
  66     @Override
  67     public Kind getKind() {
  68         return Tree.Kind.IMPORT_ENTRY;
  69     }
  70 
  71     @Override
  72     public <R,D> R accept(final TreeVisitor<R,D> visitor, final D data) {
  73         return visitor.visitImportEntry(this, data);
  74     }
  75 
  76     @Override
  77     public long getStartPosition() {
  78         return startPos;
  79     }
  80 
  81     @Override
  82     public long getEndPosition() {
  83         return endPos;
  84     }
  85 
  86     @Override
  87     public IdentifierTree getModuleRequest() {
  88         return moduleRequest;
  89     }
  90 
  91     @Override
  92     public IdentifierTree getImportName() {
  93         return importName;
  94     }
  95 
  96     @Override
  97     public IdentifierTree getLocalName() {
  98         return localName;
  99     }
 100 }