001/*
002 * Copyright (C) 2009-2011 Mathias Doenitz
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
017package org.parboiled.trees;
018
019import org.parboiled.common.ImmutableList;
020
021/**
022 * A simple immutable implementation of the {@link BinaryTreeNode} interface.
023 *
024 * @param <T> the actual implementation type of this ImmutableBinaryTreeNode
025 */
026public class ImmutableBinaryTreeNode<T extends BinaryTreeNode<T>> extends ImmutableTreeNode<T>
027        implements BinaryTreeNode<T> {
028
029    private final T left;
030    private final T right;
031
032    @SuppressWarnings({"unchecked"})
033    public ImmutableBinaryTreeNode(T left, T right) {
034        super(left == null ?
035                right == null ? ImmutableList.<T>of() : ImmutableList.of(right) :
036                right == null ? ImmutableList.of(left) : ImmutableList.of(left, right));
037        this.left = left;
038        this.right = right;
039    }
040
041    public T left() {
042        return left;
043    }
044
045    public T right() {
046        return right;
047    }
048
049}