小编Dra*_*gon的帖子

如何使用Epsilon的assertEquals在JUnit中断言两个双精度?

不推荐使用双精度的assertEquals.我发现应该使用Epsilon的表格.这是因为不可能100%严格的双打.但无论如何我需要比较两个双打(预期和实际结果),但我不知道该怎么做.

目前我的测试看起来像:

@Test
public void testCalcPossibleDistancePercentageCount() {
    int percentage = 100;
    assertEquals("Wrong max possible value for %" + percentage, 110.42, processor.calcPossibleValue(percentage));
    percentage = 75;
    /*corresponding assertions*/
}
Run Code Online (Sandbox Code Playgroud)

以下是我收到的3个双值,我想用JUnit检查:110.42,2760.5和10931.58.JUnit测试应该如何使用断言?我在一个方法中计算得到它们:

processor.calcPossibleValue(allowed_percentage){return /*Some weird formulae here*/;}
Run Code Online (Sandbox Code Playgroud)

java junit

4
推荐指数
1
解决办法
1万
查看次数

Apache Common CLI无法识别选项

我尝试使用Common CLI解析简单的args但接收ParseException.

这是我的代码:

@SuppressWarnings("static-access")
public class CmdParsingTest {
    private static final Options options;

    static {
        options = new Options();
        options.addOption(OptionBuilder.withLongOpt("schema-file")
                .withDescription("path to schema file")
                .hasArg()
                .withArgName("schemaFile")
                .create("source"));
        options.addOption( OptionBuilder.withLongOpt("state-url")
                .withDescription("state url")
                .hasArg()
                .withArgName("stateUrl")
                .create("url"));
        options.addOption( OptionBuilder.withLongOpt("update-number")
                .withDescription("update number to start from")
                .hasArg()
                .withArgName("updateNum")
                .create("update"));
        options.addOption(OptionBuilder.withLongOpt("result-file")
                .withDescription("use given file to save result")
                .hasArg()
                .withArgName("resultFile")
                .create("result"));
    }

    public static void main(String[] args) {
        args = new String[]{
                "-source /home/file/myfile.txt",
                "-url http://localhost/state",
                "-result result.txt"};
        // create the parser
        CommandLineParser parser = new BasicParser(); …
Run Code Online (Sandbox Code Playgroud)

java command-line-arguments

4
推荐指数
1
解决办法
4528
查看次数

如何使用Hibernate实现从数据库中获取数据的通用方法?

我有添加,更新和删除实体的通用方法.但我也想使用泛型来检索数据.

这是我获取数据的方法:

public static List<ClassA> getAllClassAData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(ClassA.class);
    return criteria.list();
}

public static List<ClassB> getAllClassBData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(ClassB.class);
    return criteria.list();
}

public static List<ClassC> getAllClassCData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(ClassC.class);
    return criteria.list();
}
Run Code Online (Sandbox Code Playgroud)

我试着做这样的事情:

public static <T> List<T> getAllData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(???.class); //Here is where I'm stuck
    return criteria.list();
}
Run Code Online (Sandbox Code Playgroud)

我坚持将类作为arg传递给标准创建:

 Criteria criteria = session.createCriteria(Generic.class);
Run Code Online (Sandbox Code Playgroud)

如何让JVM知道我在标准创建过程中想要传递的类?

java generics hibernate

4
推荐指数
1
解决办法
1727
查看次数

Java:如何将默认值设置为注释,并将另一个注释作为其属性?

我有一个带有3个属性的注释:

public @interface Date {
    int day() default 1;
    int month() default 1;
    int year() default 2000;
}
Run Code Online (Sandbox Code Playgroud)

和使用先前注释作为属性的注释:

public @interface Author {
    String name();
    Date date(); //default value here
}
Run Code Online (Sandbox Code Playgroud)

如何设置属性的默认值date

java annotations

4
推荐指数
1
解决办法
1911
查看次数

JavaFX 2:自动换行在ScrollPane中不起作用

如果我将自动换行设置为标签并将其放在任何布局中 - 除非我将标签放入ScrollPane,否则自动换行工作正常.这是一个例子:

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Test");
    BorderPane borderPane = new BorderPane();
    VBox myView = new VBox();
    Label label = new Label("Lorem ipsum dolor sit amet, consectetur adipisicing elit," +
            " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim" +
            " ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip" +
            " ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate" +
            " velit esse cillum dolore …
Run Code Online (Sandbox Code Playgroud)

java javafx-2

3
推荐指数
1
解决办法
4849
查看次数

如何在流程图中显示函数参数?

以下类似于代码理解的逆向工程。所以这是函数:

void deleteTask(TaskPtr& head, const char* fullName)
{
    TaskPtr current, nodeToDelete;
    if(strcmp(head->fullName, fullName) == 0)
    {
        current = head;
        head = head->next;
        delete(current->address);
        delete(current);
        return;
    }

    for(current = head; current != NULL; current = current->next)
    {
        if(strcmp(current->next->fullName, fullName) == 0)
        {
            nodeToDelete = current->next;
            current->next = nodeToDelete->next;
            delete(nodeToDelete->address);
            delete(nodeToDelete);
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在流程图(框图)中显示headfullName参数?

flowchart

3
推荐指数
1
解决办法
6439
查看次数

在GUI开发期间替代Java中的多继承

我遇到了以下困境:Java中没有多重继承,但我需要它,如何避免它?

以下是我开始考虑它的原因.

我需要一个具有几个特定属性和行为的文本框(处理焦点和模糊事件).我毫不犹豫地开发了DecorativeTextBox:

public class DecoratedTextBox extends TextBox implements FocusHandler, BlurHandler {
    public void onBlur(BlurEvent event) {
        //cool feature implementation
    }

    public void onFocus(FocusEvent event) {
        //another cool feature
    }

    //other cool features
}
Run Code Online (Sandbox Code Playgroud)

我的GUI开始看起来很好,但我没有考虑PasswordTextBox.它还必须具有与DecoratedTextBox相同的属性和行为.但是PasswordTextBox本身就是TextBox,它实际上是另一个类层次结构分支.我立刻记得,如果TextArea还拥有所有那些很酷的属性和行为等,那将会很棒.

那么我的设计有什么问题导致对多重继承的想法呢?必须采取哪些措施来满足上述要求?

一些澄清

因此,我不得不继承PasswordTextBox,TextArea等以利用它们的功能(这些类来自GWT库).但我无法理解如何在这里编织构图.

更新

纠正我如果我理解安德斯约翰森以错误的方式说的话.

解决方案应如下所示:

public class DecoratedTextBox extend AbstractEventHandler {
    private TextBox textBox;

    //wrap TextBox methods
    public String getText() {
        return textBox.getText();
    }
}

public class DecoratedPasswordTextBox  extend AbstractEventHandler {
    private PasswordTextBox passwordTextBox;

    //wrap TextBox methods
    //...
}

public class DecoratedTextArea  extend AbstractEventHandler …
Run Code Online (Sandbox Code Playgroud)

java gwt

2
推荐指数
1
解决办法
454
查看次数

JScrollPane不添加组件

我扩展了JScrollPane并向其添加了几个组件.当我向JFrame添加滚动窗格时,不会显示任何组件.如果我的类扩展,例如,JPanel,然后我将它添加到一个独立的JScrollPane,一切正常.我无法理解这种行为.谁能解释我,为什么会这样?

以下是两种变体(一种有效,另一种无效):

此变体不起作用:

public class MainScrollPanel extends JScrollPane {

    private JPanel verticalPanel;

    public MainScrollPanel() throws IOException, ParseException {
        initGUI();
        readData();
    }

    private void initGUI() {
        verticalPanel = new JPanel();
        verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));

        add(verticalPanel);
        setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    }

    private void readData() throws IOException, ParseException {
        //read data
        //...
        for(NewData message : messages) {
            verticalPanel.add(new JLabel(message.getMessage()));
        }
    }
}

public class MainGUI extends JFrame {
    private MainScrollPanel mainPanel;

    public MainGUI() throws IOException, ParseException {
        super("Scroll app");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        mainPanel = new MainScrollPanel();
        getContentPane().add(mainPanel);

        setSize(200, …
Run Code Online (Sandbox Code Playgroud)

java swing

2
推荐指数
1
解决办法
2631
查看次数

java.util.Date是否将日期表示为系统日期?

我需要用Java获取真实世界的日期和时间.我用:

Date date = new Date();
Run Code Online (Sandbox Code Playgroud)

但我不确定这不仅仅是系统时间.我不需要依赖PC本地日期和时间.

如果是这样,那么有什么方法可以从中抽象出来吗?我的意思是我需要正确的时间和日期.如果今天是2012年5月1日,并且用户在2000年12月1日更改了(可能存在系统错误),则不应影响业务逻辑.那么有没有替代方法来实现这一目标?

java

1
推荐指数
1
解决办法
481
查看次数

如何用Java替换.sh中的单行?

我有一个.sh脚本property=value.随它去:

some_property ="有些价值"

脚本中使用属性的值,并在Java代码中启动脚本.我想动态更改此属性的值.我尝试使用replaceFirst()方法,但我不知道使用regexp正确替换它的"some_property"的实际值.

如何编辑.sh文件并替换"some_property="some_property=my_value?开头的单行?顺便说一下,文件中有几个地方可以满足模式"some_property =",所以我需要更改第一次出现.

java

1
推荐指数
1
解决办法
266
查看次数

为应用程序选择什么样的swing布局结构?

这是我实际想要放在面板上的内容:

第一个逻辑块:

单选按钮1文本字段图标按钮

单选按钮2文本字段图标按钮

复选框

第二个逻辑块:

标签微调器

        按键

我的第一个决定是制作垂直框布局并为每个逻辑块放置两个水平框布局.但问题在于这些块,选择哪种布局来描述这种结构?我不喜欢GridBagLayout - 它非常复杂且难以理解,特别是当代码不是你的时候.目前我看到可以使用Flow Layout和Grid Layout.但是,例如,网格布局将按钮拉伸到单元格的宽度,如果按钮只有图标,那么它看起来很奇怪.

希望你能告诉我一些事情.

java layout swing jcomponent layout-manager

1
推荐指数
1
解决办法
215
查看次数

如果在类初始化期间调用重写的基本方法,则实例字段不会初始化

有一个基类ServerAdapter:

public abstract class ServerAdapter {
    public ServerAdapter() {
        initGUI();
    }

    protected abstract void initGUI();
}
Run Code Online (Sandbox Code Playgroud)

还有一个继承的子类ServerAdapter:

public abstract class LinuxServerAdapter extends ServerAdapter {
    protected CheckBox key = new CheckBox();

    public LinuxServerAdapter() {
        super();
    }

    @Override
    public void initGUI() {
        //NPE is thrown here, because key = null
        key.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                //Something happens here
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

继承LinuxServerAdapter的结束类:

public class MyLinuxServerAdapter extends LinuxServerAdapter {

    public MyLinuxServerAdapter() {
        super();
    } …
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
93
查看次数

Matcher在与regexp模式类似的文本中找不到任何内容

每次我需要使用regexp时,我都会遇到类似的问题.他们只是不为我工作,我不知道我做错了什么.

我有一个文字行:

插入base(db)arg(任何可能的int);

这些行也可能包含最后的评论.我需要的是找到这样的线条.以下是我尝试这样做的方法:

@Test
public void test() {
    String UPDATE_REGEXP = "insert into base (db) arg (\\d*+)";
    String sample = "insert into base (db) arg (100);";
    Pattern pattern = Pattern.compile(UPDATE_REGEXP);

    Matcher matcher = pattern.matcher(sample);

    if(matcher.find()) {
        System.out.println(matcher.group());
    } else {
        fail();
    }
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过最简单的正则表达式:

String UPDATE_REGEXP ="insert into base(db)arg";

但是这个测试总是失败.我的错在哪里?

java regex

0
推荐指数
1
解决办法
93
查看次数