| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2006 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.jdt.astview.views; |
| 16 | |
| 17 | import java.util.Objects; |
| 18 | |
| 19 | import org.eclipse.swt.graphics.Image; |
| 20 | |
| 21 | |
| 22 | public class GeneralAttribute extends ASTAttribute { |
| 23 | |
| 24 | private final Object fParent; |
| 25 | private final String fLabel; |
| 26 | private final Object[] fChildren; |
| 27 | |
| 28 | public GeneralAttribute(Object parent, String name, Object value) { |
| 29 | fParent= parent; |
| 30 | fLabel= name + ": " + String.valueOf(value); |
| 31 | fChildren= EMPTY; |
| 32 | } |
| 33 | |
| 34 | public GeneralAttribute(Object parent, String label) { |
| 35 | fParent= parent; |
| 36 | fLabel= label; |
| 37 | fChildren= EMPTY; |
| 38 | } |
| 39 | |
| 40 | public GeneralAttribute(Object parent, String name, Object[] children) { |
| 41 | fParent= parent; |
| 42 | if (children == null) { |
| 43 | fLabel= name + ": null"; |
| 44 | fChildren= EMPTY; |
| 45 | } else if (children.length == 0) { |
| 46 | fLabel= name + " (0)"; |
| 47 | fChildren= EMPTY; |
| 48 | } else { |
| 49 | fChildren= createChildren(children); |
| 50 | fLabel= name + " (" + String.valueOf(fChildren.length) + ')'; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | private Object[] createChildren(Object[] children) { |
| 55 | ASTAttribute[] res= new ASTAttribute[children.length]; |
| 56 | for (int i= 0; i < res.length; i++) { |
| 57 | Object child= children[i]; |
| 58 | String name= String.valueOf(i); |
| 59 | res[i]= Binding.createValueAttribute(this, name, child); |
| 60 | } |
| 61 | return res; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public Object getParent() { |
| 66 | return fParent; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Object[] getChildren() { |
| 71 | return fChildren; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public String getLabel() { |
| 76 | return fLabel; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Image getImage() { |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * @see java.lang.Object#equals(java.lang.Object) |
| 86 | */ |
| 87 | @Override |
| 88 | public boolean equals(Object obj) { |
| 89 | if (this == obj) |
| 90 | return true; |
| 91 | if (obj == null || !obj.getClass().equals(getClass())) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | GeneralAttribute other= (GeneralAttribute) obj; |
| 96 | if (!Objects.equals(fParent, other.fParent)) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | if (!Objects.equals(fLabel, other.fLabel)) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * @see java.lang.Object#hashCode() |
| 109 | */ |
| 110 | @Override |
| 111 | public int hashCode() { |
| 112 | return (fParent != null ? fParent.hashCode() : 0) |
| 113 | + (fLabel != null ? fLabel.hashCode() : 0); |
| 114 | } |
| 115 | } |
| 116 |
Members