@@ -50,70 +50,70 @@ template <typename E> class ArrayIterator;
5050
5151template <typename E>
5252class ArrayIterator {
53- public:
54- ArrayIterator (const Array<E> *p_vec, int pos) : _pos(pos), _p_vec(p_vec) {
55- }
56- boolean operator !=(const ArrayIterator<E> &other) const {
57- return _pos != other._pos ;
58- }
59- E operator *() const {
60- return _p_vec->get (_pos);
61- }
62- const ArrayIterator<E> &operator ++() {
63- ++_pos;
64- return *this ;
65- }
66- private:
67- int _pos;
68- const Array<E> *_p_vec;
69- };
53+ public:
54+ ArrayIterator (const Array<E> *p_vec, int pos) : _pos(pos), _p_vec(p_vec) {
55+ }
56+ boolean operator !=(const ArrayIterator<E> &other) const {
57+ return _pos != other._pos ;
58+ }
59+ E operator *() const {
60+ return _p_vec->get (_pos);
61+ }
62+ const ArrayIterator<E> &operator ++() {
63+ ++_pos;
64+ return *this ;
65+ }
66+ private:
67+ int _pos;
68+ const Array<E> *_p_vec;
69+ };
7070
7171template <typename E>
7272class Array {
73- private:
74- std::vector<E> original;
75- public:
76- Array () {
77- }
78- Array (std::initializer_list<E> list) {
79- typename std::initializer_list<E>::iterator it;
80- for (it = list.begin (); it != list.end (); ++it) {
81- original.push_back (*it);
82- }
83- this ->length = original.size ();
84- }
85- ~Array () {};
86- int length;
87- ArrayIterator<E> begin () const {
88- return ArrayIterator<E>(this , 0 );
89- }
90- ArrayIterator<E> end () const {
91- return ArrayIterator<E>(this , this ->length );
92- }
93- public:
94- void push (E e) {
95- original.push_back (e);
96- this ->length = original.size ();
97- }
98- E get (const int index) const {
99- return (E) original.at (index);
100- }
101- string toString () {
102- return (string ) " " ;
103- }
104- public:
105- E &operator [](const int index) {
106- return this ->original .at (index);
107- }
108- Array<E> operator +=(const std::initializer_list<E> &list) {
109- typename std::initializer_list<E>::iterator it;
110- for (it = list.begin (); it != list.end (); ++it) {
111- original.push_back (*it);
112- }
113- this ->length = original.size ();
114- return *this ;
115- }
116- };
73+ private:
74+ std::vector<E> original;
75+ public:
76+ Array () {
77+ }
78+ Array (std::initializer_list<E> list) {
79+ typename std::initializer_list<E>::iterator it;
80+ for (it = list.begin (); it != list.end (); ++it) {
81+ original.push_back (*it);
82+ }
83+ this ->length = original.size ();
84+ }
85+ ~Array () {};
86+ int length;
87+ ArrayIterator<E> begin () const {
88+ return ArrayIterator<E>(this , 0 );
89+ }
90+ ArrayIterator<E> end () const {
91+ return ArrayIterator<E>(this , this ->length );
92+ }
93+ public:
94+ void push (E e) {
95+ original.push_back (e);
96+ this ->length = original.size ();
97+ }
98+ E get (const int index) const {
99+ return (E) original.at (index);
100+ }
101+ string toString () {
102+ return (string ) " " ;
103+ }
104+ public:
105+ E &operator [](const int index) {
106+ return this ->original .at (index);
107+ }
108+ Array<E> operator +=(const std::initializer_list<E> &list) {
109+ typename std::initializer_list<E>::iterator it;
110+ for (it = list.begin (); it != list.end (); ++it) {
111+ original.push_back (*it);
112+ }
113+ this ->length = original.size ();
114+ return *this ;
115+ }
116+ };
117117
118118namespace Java {
119119 namespace Lang {
@@ -131,46 +131,96 @@ namespace Java {
131131
132132 class Object {
133133 protected:
134- Object &clone ();
135- void finalize ();
134+ /* *
135+ * Return a copy of this Object
136+ * Not support this function yet
137+ * @return
138+ */
139+ // Object &clone();
140+
141+ /* *
142+ * Not support this function yet
143+ */
144+ // void finalize();
145+
136146 public:
137- string toString ();
138- boolean equals (const Object &obj) const {
139- if (this ->hashCode () == obj.hashCode ()) {
147+ /* *
148+ * A string representation of the object.
149+ * @return string
150+ */
151+ virtual string toString () const {
152+ return string_from_int (this ->hashCode ());
153+ }
154+
155+ /* *
156+ * Support compare two Object through hashCode()
157+ * @param obj
158+ * @return
159+ */
160+ virtual boolean equals (const Object &o) const {
161+ if (this ->hashCode () == o.hashCode ()) {
140162 return true ;
141163 }
142164 return false ;
143165 }
144- Class<Object> getClass () {
145- // This method is only available in Java
146- // should not be supported in this library
147- }
148- int hashCode () const {
149- return (intptr_t ) std::addressof (*this );
150- }
151- void notify () {
152- // TODO
153- }
154- void notifyAll () {
155- // TODO
156- }
157- string toString () const {
158- return (string) " " ;
159- }
160- void wait () {
161- // TODO
162- }
163- void wait (long timeout) {
164- // TODO
165- }
166- void wait (long timeout, int nanos) {
167- // TODO
166+
167+ /* *
168+ * Not support this function yet
169+ */
170+ // Class<Object> getClass();
171+
172+ /* *
173+ * A hash code value for this object.
174+ * @return int
175+ */
176+ virtual int hashCode () const {
177+ long element = (intptr_t ) std::addressof (*this );
178+ int elementHash = (int )(element ^ (element >> 32 ));
179+
180+ return elementHash;
168181 }
182+
183+ /* *
184+ * Not support this function yet
185+ */
186+ // void notify()
187+
188+ /* *
189+ * Not support this function yet
190+ */
191+ // void notifyAll();
192+
193+ /* *
194+ * Not support this function yet
195+ */
196+ // void wait();
197+
198+ /* *
199+ * Not support this function yet
200+ */
201+ // void wait(long timeout);
202+
203+ /* *
204+ * Not support this function yet
205+ */
206+ // void wait(long timeout, int nanos);
207+
208+ /* *
209+ * Compare two object is equal or not
210+ * @param target
211+ * @return boolean
212+ */
169213 boolean operator ==(const Object &target) const {
170- return this ->equals ( target);
214+ return this ->equals (target);
171215 }
216+
217+ /* *
218+ * Compare two object is not equal or not
219+ * @param target
220+ * @return boolean
221+ */
172222 boolean operator !=(const Object &target) const {
173- return !this ->equals ( target);
223+ return !this ->equals (target);
174224 }
175225 };
176226 }
0 commit comments