Checkstyle working differently for same condition - java

On applying check style i am getting " hides a field" if the name of formal and actual parameters are same.
private String limitedDimensionId;
/**
* Sets the limited dimension id.
*
* #param limitedDimensionId
* the new limited dimension id
*/
public void setLimitedDimensionId(final String limitedDimensionId) {
this.limitedDimensionId = limitedDimensionId;
}
However i am not getting the same issue in the following case:
private boolean fallBack;
/**
* #param isFallBack
* the isFallBack to set
*/
public void setFallBack(final boolean isFallBack) {
this.fallBack = isFallBack;
}
Both the conditions appear same to me. Still the discrepancy. Usually i change the name of the parameter variable to resolve this check style issue. But looking at the other case i am getting a hint that a more elegant solution is available. Any insights?

The variable names are different:
fallBack vs isFallBack
Usually i change the name of the parameter variable to resolve this check style issue
That's correct solution.

I would agree that giving them different names is more appropriate, however the "this" keyword in the "this.limitedDimensionid" should avoid the "hides a field" error. That's what it's for...

Related

method clear in class Screen cannot be applied to given types error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I keep getting the error message
method clear in class Screen cannot be applied to given types;
required: boolean
found: no arguments
reason: actual and formal argument list differ in length
For clear(),
I'm trying to do an exercise from a workbook that wants me to use the following headers and fill out the methods and constructor. The actual function of clear() doesn't matter, just that I use the headers and call the clear method if xRes x yRes > 2000000)
The question,
Exercise 3.55 Given the following class (only shown in fragments here),
public class Screen
{
public Screen(int xRes, int yRes)
{ ...
}
public int numberOfPixels()
{ ...
}
public void clear(boolean invert)
{ ...
}
}
write some lines of Java code that create a Screen object. Then call its clear
method if (and only if) its number of pixels is greater than two million. (Don’t
worry about things being logical here; the goal is only to write something that is
syntactically correct—i.e., that would compile if we typed it in.)
My code,
/**
* Write a description of class Screen here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class Screen
{
// instance variables - replace the example below with your own
private int xRes;
private int yRes;
private boolean invert;
/**
* Constructor for objects of class Screen
*/
public Screen(int xRes, int yRes, boolean invert)
{
// initialise instance variables
this.xRes = xRes;
this.yRes = yRes;
if((xRes * yRes) > 2000000)
{
clear(); // this is where i get my error
}
}
/**
* An example of a method - replace this comment with your own
*
* #param y a sample parameter for a method
* #return the sum of x and y
*/
public int numberOfPixels()
{
// put your code here
return xRes * yRes;
}
public void clear(boolean invert)
{
this.invert = invert;
}
}
The clear metod has a strange implementation right now as it doesn't clear anything but maybe you will get to fixing that later.
To call the method in it's current form you need to provide an argument of true or false.
Like this:
clear(true);
You need to pass a boolean value while invoking the method. It depends on you want do you want to pass. Call your method like this:
boolean a = true; // False can also be given depending on need
if((xRes * yRes) > 2000000)
{
clear(a);
}
You can pass either true or false by changing the value of a.

Eclipse Object Info Box on Custom Objects

I couldn't find how to do this anywhere else online, though I'm sure it's really easy to do. I'm primarily self taught though, and I'd like to start learning to document my code properly. This "yellow box" that pops up in eclipse with information about the method - I want it to pop up on a custom object. For my example below I have a custom class called "System Properties" and a method called "getOs" but when I hover that option, no information comes up. How do I add information to my object?
This picture shows the yellow box
This picture shows the lack of a "yellow box" on my object
and then finally my custom objects code...
public class SystemProperties {
private String os;
public SystemProperties() {
this.os = setOs();
}
private String setOs() {
String osName = System.getProperty("os.name");
if(osName.toLowerCase().contains("window"))
return "Windows";
else if(osName.toLowerCase().contains("mac"))
return "Mac";
else
return "Linux";
}
/**
* Method to grab the OS the user is running from
* #return String - the os
*/
public String getOs() {
return this.os;
}
}
Thank you in advance for your time and knowledge. :)
EDIT:
When I import the project of the custom object, it works just fine. It only doesn't work when I export the project of the custom class to a jar file and then use that instead. Do I have to click an option on the export screen?
Eclipse take the info from the notes above the methods in the built in objects.
see this:
/**
* Returns <tt>true</tt> if this map contains a mapping for the specified
* key. More formally, returns <tt>true</tt> if and only if
* this map contains a mapping for a key <tt>k</tt> such that
* <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be
* at most one such mapping.)
*
* #param key key whose presence in this map is to be tested
* #return <tt>true</tt> if this map contains a mapping for the specified
* key
* #throws ClassCastException if the key is of an inappropriate type for
* this map
* (optional)
* #throws NullPointerException if the specified key is null and this map
* does not permit null keys
* (optional)
*/
boolean containsKey(Object key);
You can do the same to the methods of your own objects.

Java 3.0 get distinct values from a column mongodb

I am really struggling here and have looked at other questions but just cant seem to get the answer I need.
What I am trying to do is pull through all the unique values of a column and then iterate through them and add them to an array. Ending up with the one column being stored in my array, but one of each value that exists not the multiple like there currently is.
Every time I try and do .distinct it asks me for the return class I have tried many different class but it just doesn't seem to work... Code is below any help would be appreciated.
public static void MediaInteraction() {
//Storing data from MediaInteraction in MediaArray
//BasicDBObject Query = new BasicDBObject();
//Query.put("consumerid", "");
MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid", (What do I put here?)).iterator();
while (cursormedia.hasNext()) {
System.out.println(cursormedia.next());
MediasessionID.add(cursormedia.next());
}
System.out.println("Media Array Complete");
System.out.println(MediasessionID.size());
}
The change that you probably want to introduce shall be somewhat like -
MongoCursor<Document> cursormedia = collectionmedia.distinct("consumerid",
<ConsumerId-DataType>.class).iterator(); //please replace the consumerId field's datatype here
Also from the docs -
/**
* Gets the distinct values of the specified field name.
*
* #param fieldName the field name
* #param resultClass the class to cast any distinct items into.
* #param <TResult> the target type of the iterable.
* #return an iterable of distinct values
* #mongodb.driver.manual reference/command/distinct/ Distinct
*/
<TResult> DistinctIterable<TResult> distinct(String fieldName, Class<TResult> resultClass);
So in your example, if you are trying to attain cursor for Document you probably want to use Document.class in the above suggested code.
Edit - Also the fact that you are calling cursormedia.next() twice the count of your MediasessionID would be halved. Suggest you do that(.next) once improving it further to obtain results.

Sonar violation on "disallowed assignment of parameters"

I have the following code and I got an sonar violation error: disallowed assignment of parameters
What is the best way to fix this?
/**
* #param lastAccessTime the lastAccessTime to set
*/
public void setLastAccessTime(Date lastAccessTime)
{
this.lastAccessTime = lastAccessTime == null ? null : new Date(lastAccessTime.getTime());
}
I suspect one of 2 things are happening here:
1 - There is a bug in the checkstyle plugin
2 - The code sonar analysed is not quite the code you posted here
I believe that violation should apply in the following case:
/**
* #param lastAccessTime the lastAccessTime to set
*/
public void setLastAccessTime(Date lastAccessTime)
{
lastAccessTime = lastAccessTime == null ? null : new Date(lastAccessTime.getTime());
}
So when you are reassigning the method parameter it would be expected, but in your example you are not, you are assigning it to a class field so it should be ok.
Try changing the method parameter to final and see if you still see the violation.

how to copy javadoc from variables to constructor

I am having some problems with Javadoc. I have written documentation for variables of a class. And then I want to use that same javaDoc in the constructor. I didn't seem to be able to use #link or #see for this purpose (Well, Netbeans didn't show the result I liked).
It seems like a hassle to copy-paste everything, so is there a tag/parameter to copy javaDoc?
Here is the example:
/**
* The id for identifying this specific detectionloop. It is assumed the
* Detectionloops are numbered in order, so Detectionloop '2' is always next to
* Detectionloop '1'.
*/
private int id;
/**
* Constructor for a detectionloop. Detectionloops are real-world sensors
* that register and identify a kart when it passes by. Please note that
* this class is still under heavy development and the parameters of the
* constructor may change along the way!
*
* #param id The id for identifying this specific detectionloop. It is assumed
* the Detectionloops are numbered in order, so Detectionloop '2' is always
* next to Detectionloop '1'.
* #param nextID The id of the next detectionloop is sequense.
* #param distanceToNext The distance in meters to the next detectionloop.
*/
DetectionLoop(int id, int nextID, int distanceToNext) {
this.distanceToNext = distanceToNext;
this.id = id;
if (Detectionloops.containsKey(id)) {
throw new IllegalArgumentException("Detectionloop " + this.id
+ " already exist, please use a unused identification!");
} else {
Detectionloops.put(this.id, this);
}
}
This is unfortunately impossible using standard Javadoc. As a workaround, you could use the #link tag to reference the field, and then people could click the link to get at its documentation. This would require a click, but at least you don't have to maintain redundant documentation:
/**
* ...
* #param id the value for {#link #id}
The only other way of solving this that I know of is to write a custom doclet, which would allow you to define your own tag for your purpose.

Categories