001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.js.translate.declaration;
018    
019    import com.google.dart.compiler.backend.js.ast.*;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.kotlin.js.translate.context.TranslationContext;
023    import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
024    import org.jetbrains.kotlin.name.FqName;
025    
026    import java.util.AbstractList;
027    import java.util.List;
028    
029    public class DefineInvocation {
030    
031        /* package */
032        @NotNull
033        static DefineInvocation create(
034                @NotNull FqName packageFqName,
035                @Nullable JsExpression initializer,
036                @NotNull JsObjectLiteral members,
037                @NotNull TranslationContext context
038        ) {
039            return new DefineInvocation(initializer == null ? JsLiteral.NULL : initializer,
040                                 new JsDocComment(JsAstUtils.LENDS_JS_DOC_TAG, context.getQualifiedReference(packageFqName)),
041                                 members);
042        }
043    
044        @NotNull
045        private JsExpression initializer;
046        @NotNull
047        private final JsDocComment jsDocComment;
048        @NotNull
049        private final JsObjectLiteral membersObjectLiteral;
050    
051        private DefineInvocation(
052                @NotNull JsExpression initializer,
053                @NotNull JsDocComment jsDocComment,
054                @NotNull JsObjectLiteral membersObjectLiteral
055        ) {
056            this.initializer = initializer;
057            this.jsDocComment = jsDocComment;
058            this.membersObjectLiteral = membersObjectLiteral;
059        }
060    
061        @NotNull
062        public JsExpression getInitializer() {
063            return initializer;
064        }
065    
066        public void setInitializer(@NotNull JsExpression initializer) {
067            this.initializer = initializer;
068        }
069    
070        @NotNull
071        public List<JsPropertyInitializer> getMembers() {
072            return membersObjectLiteral.getPropertyInitializers();
073        }
074    
075        @NotNull
076        public List<JsExpression> asList() {
077            return new AbstractList<JsExpression>() { // because initializer is mutable
078                @NotNull
079                @Override
080                public JsExpression get(int index) {
081                    switch (index) {
082                        case 0:
083                            return initializer;
084                        case 1:
085                            return jsDocComment;
086                        case 2:
087                            return membersObjectLiteral;
088                    }
089                    throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size());
090                }
091    
092                @Override
093                public int size() {
094                    return 3;
095                }
096            };
097        }
098    
099    }