Remove exported everywhere.
[SquirrelJME.git] / modules / tool-classfile / src / main / java / net / multiphasicapps / classfile / InstructionJumpTargets.java
blob70d0e5146daee56a6e53a5aae8a6854d5d3b4ad8
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package net.multiphasicapps.classfile;
12 import cc.squirreljme.runtime.cldc.debug.Debugging;
13 import cc.squirreljme.runtime.cldc.util.SortedTreeSet;
14 import java.lang.ref.Reference;
15 import java.lang.ref.WeakReference;
16 import java.util.Arrays;
17 import java.util.Set;
19 /**
20 * This represents the targets of jumps that an instruction may jump to.
22 * @since 2019/03/30
24 public final class InstructionJumpTargets
26 /** Normal jumps. */
27 private final InstructionJumpTarget[] _normal;
29 /** Exceptional jumps. */
30 private final InstructionJumpTarget[] _exception;
32 /** String representation. */
33 private Reference<String> _string;
35 /** Hashcode. */
36 private int _hash;
38 /**
39 * Initializes empty jump targets.
41 * @since 2019/04/11
43 public InstructionJumpTargets()
45 this._normal = new InstructionJumpTarget[0];
46 this._exception = new InstructionJumpTarget[0];
49 /**
50 * Initializes the jump targets.
51 * @param __n Normal jumps.
52 * @param __e Exceptional jumps.
53 * @throws NullPointerException On null arguments.
54 * @since 2019/03/30
56 public InstructionJumpTargets(InstructionJumpTarget[] __n,
57 InstructionJumpTarget[] __e)
58 throws NullPointerException
60 if (__n == null || __e == null)
61 throw new NullPointerException("NARG");
63 // Load into sorted sets
64 Set<InstructionJumpTarget> nrm = new SortedTreeSet<>(),
65 exe = new SortedTreeSet<>();
67 // Add normals
68 for (InstructionJumpTarget i : __n)
69 if (i == null)
70 throw new NullPointerException("NARG");
71 else
72 nrm.add(i);
74 // Add exceptional
75 for (InstructionJumpTarget i : __e)
76 if (i == null)
77 throw new NullPointerException("NARG");
78 else
79 exe.add(i);
81 // Set
82 this._normal = nrm.<InstructionJumpTarget>toArray(
83 new InstructionJumpTarget[nrm.size()]);
84 this._exception = exe.<InstructionJumpTarget>toArray(
85 new InstructionJumpTarget[exe.size()]);
88 /**
89 * {@inheritDoc}
90 * @since 2019/03/30
92 @Override
93 public final boolean equals(Object __o)
95 throw Debugging.todo();
98 /**
99 * Obtains the given jump target.
101 * @param __i The index.
102 * @return The target jump target.
103 * @since 2019/03/31
105 public final InstructionJumpTarget get(int __i)
107 InstructionJumpTarget[] normal = this._normal;
108 int numnormal = normal.length;
110 // Treat both arrays as a single part
111 if (__i >= numnormal)
112 return this._exception[__i - numnormal];
113 return this._normal[__i];
117 * {@inheritDoc}
118 * @since 2019/03/30
120 @Override
121 public final int hashCode()
123 throw Debugging.todo();
127 * Checks if any of the jump targets has an address which follows the
128 * given address.
130 * @param __pc The address to check.
131 * @return True if any address in the jump targets has an address which
132 * is higher than this address.
133 * @since 2019/03/30
135 public final boolean hasLaterAddress(int __pc)
137 for (int i = 0, n = this.size(); i < n; i++)
138 if (this.get(i).target() > __pc)
139 return true;
141 return false;
145 * Checks if any of the jump targets has an address which is or follows
146 * the given address.
148 * @param __pc The address to check.
149 * @return True if any address in the jump targets has an address which
150 * is the same or higher than this address.
151 * @since 2019/04/11
153 public final boolean hasSameOrLaterAddress(int __pc)
155 for (int i = 0, n = this.size(); i < n; i++)
156 if (this.get(i).target() >= __pc)
157 return true;
159 return false;
163 * Returns if this is empty or not.
165 * @return If this is empty or not.
166 * @since 2019/03/31
168 public final boolean isEmpty()
170 return this.size() == 0;
174 * Returns if this is an exception jump.
176 * @param __i The index to get.
177 * @return If this is an exception index.
178 * @since 2019/03/31
180 public final boolean isException(int __i)
182 return __i >= this._normal.length;
186 * Returns the number of jump targets.
188 * @return The number of jump targets.
189 * @since 2019/03/31
191 public final int size()
193 return this._normal.length + this._exception.length;
197 * {@inheritDoc}
198 * @since 2019/03/30
200 @Override
201 public final String toString()
203 Reference<String> ref = this._string;
204 String rv;
206 if (ref == null || null == (rv = ref.get()))
207 this._string = new WeakReference<>((rv =
208 "[N:" + Arrays.asList(this._normal) +
209 ", E: " + Arrays.asList(this._exception) + "]"));
211 return rv;