Win a copy of Terraform in Action this week in the Cloud forum!

Mike Cheung

Ranch Hand
+ Follow
since Feb 01, 2013
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mike Cheung

The first two problems were solved by annotating the test class with but the cast from PowerMockMaker to MockMaker is still an issue.
3 years ago
I'm trying to do a unit test on my socket server code.  I'm using Mockito but it blows up when the SUT runs to the following line because this is a final method.

I'm new to PowerMock and I am trying to use it but I am facing various issues no matter what I tried.  If someone can help review the following and offer some help that'd be great.

Here is the code used for testing.

First problem
It is having a problem with the following line.

This is the full stack trace.

Second problem
If I comment out the logger I am getting the following.

Third problem
If I remove the 'extends AbstractTestNGSpringContextTests' bit on the class I am still getting the following.  At this point I'm not sure what else I can try.

3 years ago
Hi, I'm writing an Angular 2 application which does allow passing objects to 'properties'. The element <my-element> in the code snippet is the root Angular Component, and what looks like to be an attribute named model is actually a property of the <my-element> Component.

Angular is designed to allow this within the Angular application tree (ie you can do this between components)!#other-bindings

The following example demonstrates this in the HeroDetailComponent component as defined in app/hero-detail.component.ts.

The line "inputs: ['hero']" means we can send a Hero object to the property 'hero' like this which we can see in the app/app.component.ts file.

The problem however is while Angular does allow this within components, it doesn't allow this to be done from a non Angular component (ie from index.html) yet. Can you highlight what problems this may introduce as I'm interested to know what are the potential problems for doing so. And of course also how can we actually do this? Thanks.
Hi I need to do the following in an HTML file.
1) Create a Javascript variable storing a JSON object.
2) Pass this variable's value to a custom HTML element's attribute.

I created the following file.

However all I'm getting from this is the text "textJson" being passed in rather than "jsonObject" : {"name": "myObject", "id": 89}. Any idea how I can achieve this? Thanks.
Hi there I have a question with using Spring Data JPA. I don't like how all the examples are using List<Object[]> for native named queries. There is no type checking and makes the code harder to read. So I tried to type cast it to List<myType> instead.
The interesting bit is that the result set was created using this new type fine but the minute I try to use it via a for loop, it fails with the following.
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.EmployeeAddress

Snippet 1 - this works

Snippet 2 - this fails

The interesting bit is if I set a break point at the following line ...

The IDE shows that the variable queryResult13 is created as type List<EmployeeAddress>. However as soon as I look into the List, it shows that the elements were of type Object[] (same as what I'd get from snippet 1 above). Any idea?
Okay another way of doing it, if we are using the new Java 1.8 Function interface, can be like this. This is based on a description on how we can do this functionally from

This class is the same as before, except we no longer need the constructor to do the conversion.

Again no custom constructor required here.

The conversion is now handled via this function which is implemented as an Anonymous Class. The logic for the conversion now takes place here.

And here is how we do the transform now.

Question is which one is better?
5 years ago
Per, it has the following explanation.

Interface Function<T,R>

Type Parameters:
T - the type of the input to the function
R - the type of the result of the function

Why are we using T & R instead of X & Y? Is there any special meaning to T & R? I always forget which one is input and which one is output.
5 years ago
The only sensible way I can see is really resorting to using multiple Java classes to represent the same thing, and use the correct one depending on the scenario. For our example case, we'd have a User class that has all the fields and will be used for the user list page where we want to show all details. Then if we want to serve the comments page, we'd have to retrieve the details from database like how we normally would, and then convert it to a trimmed down class before serializing to JSON.

Normal Classes

Trimmed Down Classes

5 years ago
Hi, how do you do this elegantly say using Jackson or maybe Gson? If you are writing a web based commenting system with 2 pages only:
1) Main page showing comments and the name of the user who posted each comment.
2) Member detail page showing all information such as join date, post count, last posted date, etc for any given user.

You naturally have a situation where you'd need to serve different amount of information for the same User object depending on which page you are serving.
If I use something like Spring Data's findAll() method (ie commentRepository.findAll()), I'll be able to get obviously all the comments (assuming we have a small system here) and the users (via predefined @OneToOne annotation).
If I then serialize to JSON using Jackson, I should see something like the following.

Then if I use the following Handlebars template, I'd be able to show just only the name of the person who posted the comment, and the rest of the user details such as id, totalPosts, lastPostedOn would never even hit the browser.

And then on the other page I can use something like the following to show all the details I want for each user.

So far so good. I can use the same User model and display different level of detail depending on what I specify in the template.

But this only works if you are using a template that gets rendered on the server such as What if you now have a template that gets rendered on the client side such as handlebars.js?
If we are running the same handlebars template on the browser, the server will send all fields from the User model (ie all columns per user from the database) to the client before it gets picked out by the template on which one to use. The disadvantages are:
1) Poor performance because we are sending unnecessarily large amount of traffic to the browser.
2) Unable to hide details because if the User object contains confidential fields such as e-mail address, phone numbers, etc we don't want to send all these to the browser even if they won't be displayed at the end.

So the question is how can we do this properly? Has anyone come across this scenario? I'm hoping that I may find a way to have the Jackson 2 library to serialize only some of the fields to JSON in one case and all of the fields to JSON in another case depending on a flag which indicates which page we're serving but initial research doesn't seem like this is supported. There are custom serializers but this seems to be a bit complicated for a scenario that is supposed to be quite common. Do I have to write two User model classes now using two different native queries (one selecting some columns and another selecting more columns)? What would you do?
5 years ago
And sorry forgot to mention the code will handle non uniqueness of the data being import before trying to put it to the map.
6 years ago
Hi thanks for the comments. Persisting into database will be nice but it needs to be managed separately because I can't define at compile time a specific object type with known properties to represent the columns. It needs to allow me to load say any CSV source at runtime which structure is unknown beforehand.

I understand we can't query map like a database but I saw something available for Hazelcast and GridGain but the former doesn't do joins and the latter require you to know the object at compile time unless you buy the commercial version.

So basically you can imagine me trying to have something like Excel which allows you to import any arbitrary CSV except I want to query the data using SQL after the import.

I thought about using an embedded database but I prefer to avoid using one.

6 years ago
Hi I'm looking for a way to implement a table in memory to store data of primitive types (strings, integers, floats, etc), and would like to have a way to query data. I think using HashMap provides a good store (unless someone has a better idea), and there are 2 ways of doing this.

Assuming we have a row like this for the following example implementation.
Name, Sex, Rank, Dept, Age
Jane, Female, Manager, HR, 37

Implementation 1
In this case, we are using only one map object. We are going to store the primary key column as a key of the map, and the rest of the columns in a delimited fashion as value of the map.

Implementation 2
In this case, we are using a list of maps. We are going to store the columns of each row in a map, and then store this map in a list.

1) Which way of implementing this is better? And are there even better ways of doing this? There may be pros and cons with each approach and I'm happy to see what comments we have.
2) If I want to perform SQL like queries over this data? Is there a Java library that I can add and provides SQL syntax support? For example, the following should be possible but I haven't found such a library.


6 years ago
Hi, sorry I'm not aware of a data structure that can stores all of the following:
- Integer to represent row number
- Integer to represent column number
- Array index, if that's what you mean

Can you give some links to this method?
6 years ago
Hi Paul sorry how do you mean by the following?

mapping (row, column) -> cell number

What datatype is involved here?
6 years ago
Hi, am curious if there's a way to build a table object (ie rows x columns) that gives you the same performance when inserting either a row or a column. In other words, the time it takes to insert a row should be the same as the time it takes to insert a column.

The only way I can see myself implementing this is to use either of the following:
Option 1: An array of columns, where each column is an array of rows.
Option 2: An array of rows, where each row is an array of columns.

Here I'm using the word "array" but it can be a vector, or list.

Option 1 gives faster time to insert a new column than to insert a new row.
Option 2 gives faster time to insert a new row than to insert a new column.

I have searched all over the web looking but still comes to the same conclusion above. Is there really no way to build a table that offers you equal insert speed of either row or column?
6 years ago