Thousands separator thymeleaf: use apostrophe - formatting

I'm using thymeleaf to render my views.
Is it possible to use ' (apostrophe) as a thousands seperator?
Example: What I want is to replace WHITESPACE in the following example with a Value for an apostrophe.
<span th:text="${#numbers.formatDecimal(value, 5, 'WHITESPACE', 2, 'POINT' )}"
Is there a NumberPointType for apostrophe or another solution to achieve a formatting like this: 1'000.00

If you dig into the sourcecode of enum org.thymeleaf.util.NumberPointType you will see the only available options with standard #numbers.formatDecimal are..
public enum NumberPointType {
POINT("POINT"),
COMMA("COMMA"),
WHITESPACE("WHITESPACE"),
NONE("NONE"),
DEFAULT("DEFAULT");
...
So my preference for you is to create a custom method inside your own code, for example as a bean or a utility object, and create your own function to format the decimal as you wish.
Update
public class UIutil {
/**
* Formats a BigInteger to a thousand grouped String
* #param number
* #return
*/
public static String formatNumber (BigInteger number) {
return String.format("%,d", number);
}
}
Assign:
context.setVariable("formatter", new UIutil());
Call:
th:text="${formatter.formatNumber(value)}"

Related

Is this an anti-pattern/code smell? - Using enum/class of consts to access class properties with [] instead of using dot property

class FruitFields {
static const NAME = "name";
static const DESCRIPTION = "descriptionText";
}
class Fruit {
final string name;
final string descriptionText;
Fruit(required string id, required string descriptionText);
}
Then when I want to do something with a Fruit use the [] instead of . notation.
var banana = new Fruit('banana', 'a yellow fruit')
banana[FruitFields.DESCRIPTION] = 'a very yellow fruit'
// instead of
banana.descriptionText = 'a very yellow fruit'
Why? So I can refactor without having to think. If I want to change DESCRIPTION to SUMMARY it's easier to do a find and replace on FruitFields.DESCRIPTION verse apple.descriptionText, banana.descriptionText... where some non fruit object like car.descriptiontText could have a similarly named property I would have to watch out for.
I can't tell if this dumb or not.
The compiler can't help you to spot a typo in the value if you use strings. Using enums however, the compiler only let you use valid values. You also get an performance benefit. An enum value is just a number and it's faster to work with numbers than with strings.

How to add a UDF with variable number parameter in calcite?

I'm using Apache Calcite to validate SQL. I add tables and UDFs dynamically.
The problem is when I add a UDF with variable number parameter, the validator can not find this function.
Version of Calcite is 1.18.0
And this is my code.
TestfuncFunction.java
public class TestfuncFunction {
public String testfunc(String... arg0) {
return null;
}
}
Add UDF
Function schemafunction = ScalarFunctionImpl.create(TestfuncFunction.class),"testfunc");
SchemaPlus schemaPlus = Frameworks.createRootSchema(true);
schemaPlus.add("testfunc", schemafunction);
SQL
select testfunc(field1, field2) from test_table
testfunc is a ScalarFunction with variable number parameter,field1 and field2 are columns of test_table. So this is a legal SQL. But I got this CalciteContextException when validating:
No match found for function signature testfunc(<CHARACTER>, <CHARACTER>)
I tryed to change my sql into one parameter like this:
select testfunc(field1) from test_table
and got this exception
java.lang.AssertionError: No assign rules for OTHER defined
at org.apache.calcite.sql.type.SqlTypeAssignmentRules.canCastFrom(SqlTypeAssignmentRules.java:386)
at org.apache.calcite.sql.type.SqlTypeUtil.canCastFrom(SqlTypeUtil.java:864)
at org.apache.calcite.sql.SqlUtil.lambda$filterRoutinesByParameterType$4(SqlUtil.java:554)
...
It seems that calcite transform java array type into SqlTypeName.OTHER.
I have tryed to override method "createJavaType" in JavaTypeFactoryImpl like this:
private static class CustomJavaTypeFactoryImpl extends JavaTypeFactoryImpl {
#Override
public RelDataType createJavaType(Class clazz) {
if (clazz.isArray()) {
return new ArraySqlType(super.createJavaType(clazz.getComponentType()), true);
}
return super.createJavaType(clazz);
}
}
but it did not work.
Do Calcite support UDF with variable number parameter, and what should I do.

PHPDoc return type for trait function

I have functions like the following in my code that return the object (for chaining):
/**
* Set properties
*
* #param $name
* #param $value
*
* #return \Boka10\Page\MenuItemConfig
*/
public function __set($name, $value)
{
$this->$name = $value;
return $this;
}
I want to move this function into a trait because, well, basically it is reused all the time.
My problem is, that the return $this line makes problems in the documentation section. In my PHPDoc it says (in this example) #return \Boka10\Page\MenuItemConfig.
How do I create a "global" trait method whose return documentation contains the correct typecast? Is it possible to do that or should I just add the __set function to each class?
I am not sure if I can explain what problem I am having here ;) What exactly would be the content of the #return tag in a globally used trait if all of these objects return their own instance?
After some digging and "hacking" I checked out the tests in Psalm and found, that the following seems to be the best method to comment these cases:
/**
* #method string somefunction($name, $value)
* #property string $name
*/
class ImplementTrait {
use MagicTrait;
}
The properties and methods are documented on the implementing class.
Note: I am using Psalm to check the code quality and this is psalms accepted way:
Psalm Tests for __call annotations
Psalm tests for __get/__set annotations
I use the following approach to add auto-completion in PHPStorm when using classes that implement some interfaces.
I create an interface with a skeleton of all target Trait public methods and extend it.
Example:
Trait MySimpleTrait {
/**
* #inheritDoc
*/
public function someMethod($someArg){
return doSomethingWithSomeArg($someArg);
}
}
Interface MySimpleTraitInterface {
/**
*
* This method do some thing with <var>$someArg</>
*
* #param mixed $someArg
* #return mixed
*/
public function someMethod($someArg);
}
interface MySimpleServiceInterface extends MySimpleTraitInterface
{
}
class MySimpleService implements MySimpleServiceInterface
{
use MySimpleTrait;
}
PHPStorm showing autocomplete for method defined in Trait:
PHPStorm showing phpDoc for method defined in Trait:
Seems absurd in 2022, but I have a lot of projects with PHP 5.6 and the syntax is supposed to be compatible (i.e., no typehint in non-scalar args or return functions).

JSDOC datatype in #param

In jsdoc #param,
We can write like:
/**
* #param {Object} somebody- The employee who is responsible.
*/
function sayHello(somebody: ABC) {
alert('Hello ' + somebody);
}
If I have a class like ABC and somebody is an object of type ABC, can I write it like this,?
/**
* #param {ABC} somebody- The employee who is responsible.
*/
function sayHello(somebody: ABC) {
alert('Hello ' + somebody);
}
That is, can I give my own object type inside curly braces of param rather than giving as object? Thanks in advance...
Looking at your code you are probably using TypeScript. You have 2 options:
If ABC is an object or class you can reference it by using a JSDoc namepath (ex: myNamespaceOrModule.ABC};
If ABC is not an actual JavaScript object (ex: a TypeScript interface) than you can use JSDoc #typedef tag to document a custom tag.
Please, provide the code where the ABC class is defined for a better answer.

Why Text is preferred than String in Hive UDF java class

There is a UDF java class shown as below:
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
public class Strip extends UDF {
private Text result = new Text();
public Text evaluate(Text str) {
if (str == null) {
return null;
}
result.set(StringUtils.strip(str.toString()));
return result;
}
public Text evaluate(Text str, String stripChars) {
if (str == null) {
return null;
}
result.set(StringUtils.strip(str.toString(), stripChars));
return result;
}
}
Hive actually supports Java
primitives in UDFs (and a few other types, such as java.util.List and
java.util.Map), so a signature like:
public String evaluate(String str)
would work equally well. However, by using Text we can take advantage of object reuse,
which can bring efficiency savings, so this is preferred in general.
Can someone tell me the reason why Text is preferred? Why we could take advantage of object reuse by using Text. When we execute the following command in Hive:
hive> SELECT strip(' bee ') FROM dummy;
After that we execute another command using that Strip function, then the Strip object is created again, right? So we cannot reuse it, right?
You can reuse a Text instance by calling one of the set() methods on it. For example:
Text t = new Text("hadoop");
t.set("pig");