Skip to content

Commit 4e836c6

Browse files
author
Food Tiny
authored
Merge pull request #65 from foodtiny/task/refactor-pointer
Task/refactor pointer
2 parents 8302a95 + cfc22ab commit 4e836c6

20 files changed

Lines changed: 1107 additions & 663 deletions

File tree

java/io/Serializable/Serializable.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
namespace Java {
3131
namespace IO {
3232
class Serializable {
33+
protected:
34+
Serializable() {};
35+
virtual ~Serializable() {};
3336
};
3437
}
3538
}

java/lang/CharSequence/CharSequence.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
namespace Java {
3333
namespace Lang {
34-
class CharSequence : public virtual Object {
34+
class CharSequence : public Object {
3535
};
3636
}
3737
}

java/lang/Cloneable/Cloneable.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
namespace Java {
3131
namespace Lang {
3232
class Cloneable {
33+
34+
protected:
35+
Cloneable() {};
36+
virtual ~Cloneable() {};
3337
};
3438
}
3539
}

java/lang/Iterable/Iterable.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace Java {
3636
template <typename T>
3737
class Iterable {
3838
public:
39-
virtual Iterator<T> &iterator() const = 0;
39+
//virtual Iterator<T> &iterator() const = 0;
4040
};
4141
}
4242
}

java/lang/Object/Object.hpp

Lines changed: 142 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -50,70 +50,70 @@ template <typename E> class ArrayIterator;
5050

5151
template <typename E>
5252
class 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

7171
template <typename E>
7272
class 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

118118
namespace 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
}

java/util/AbstractCollection/AbstractCollection.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1717
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1818
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19-
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU DING, BUT NOT
2020
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2121
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2222
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2323
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2424
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
*/
2626

27+
#include "AbstractCollection.hpp"

java/util/AbstractCollection/AbstractCollection.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ using namespace Java::Lang;
3535
namespace Java {
3636
namespace Util {
3737
template <typename E>
38-
class AbstractCollection : public virtual Object, public virtual Collection<E> {
39-
public:
40-
string toString() const {
41-
return (string) "";
42-
}
38+
class AbstractCollection : public Object, public virtual Collection<E> {
39+
40+
protected:
41+
AbstractCollection() {}
42+
virtual ~AbstractCollection() {}
43+
4344
};
4445
}
4546
}

0 commit comments

Comments
 (0)