小编cru*_*ush的帖子

将int与Java中的字符常量进行比较

我有一个二进制文件,它使用4个字符集来形成具有"含义"的整数.例如,4个字节'0005',以整数形式等于808464437.

我宁愿在我的Java代码中代表这些'0005'而不是808464437.我也不想设置常量final int V0005 = 808464437.

在C++中,我可以执行以下操作:

fileStream->read((byte*) &version, 4); //Get the next bytes and stuff them into the uint32 version

switch (version) {
    case '0005':
        //do something
    case '0006':
        //do something
    case '0007':
        //do something
}
Run Code Online (Sandbox Code Playgroud)

在Java中,我的问题不是读取4个字节并将它们放入某种数据类型.我的问题是将一些数据类型与const char数组进行比较'0005'.

如何将int或其他形式与'0005'Java中的const字符数组进行比较?我需要尽可能高效地做到这一点!

java comparison binary-data

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

在使用JSX Harmony时,如何使WebStorm调整结束标记的缩进以匹配其父级?

我正在尝试从Visual Studio 2015切换到IntelliJ WebStorm 10.0.4.

经过一些谷歌搜索,我发现我需要将JavaScript语言切换到JSX Harmony,以使JSX语法工作.

现在,我发现结束标签的缩进正在成为一种真正的痛苦.

例如,如果我编写以下内容,当我关闭<div>标记时,它会将它放在缩进级别的下一行.这很好,但我希望一旦我完成标记,它会自动调整到与其匹配的开始标记相同的缩进级别.这是Visual Studio 2015中的行为.

'use strict';

var React = require('react');

var MyComponent = React.createClass({
    render: function () {
        return (
            <div>
                </div> //The indentation is wrong here after completing the tag.
        );
    }
});

module.exports = MyComponent;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我知道我可以使用重新格式化代码,Ctrl + Alt + L但我不想一直这样做.

我已经尝试过使用JSLint和JSHint,但它们似乎并不能很好地支持JSX.我也试过ESLint,但它看起来有点儿麻烦,而且,也没有解决这个问题(应该吗?).

我配置错误了吗?有没有办法在WebStorm中配置此行为?

intellij-idea jsx webstorm

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

防止DataContractSerializer中的null成员序列化

让我先说我对WCF很新,可能在这里使用了错误的术语.我的项目有两个组成部分:

  1. 包含Attachment,Extension,ReportType1和ReportType2的类的DLL
  2. 具有如下所述的OperationContract的WCF ServiceContract将XML文档反序列化为相关对象,然后将其作为JSON或XML再次序列化回客户端.

我有一个XML模式,如下所示:

<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xsd:element name="Attachment">
        <xsd:complexType>
            <xsd:all>
                <xsd:element name="Extension" type="Extension" minOccurs="0" />
            </xsd:all>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType>
        <xsd:sequence name="Extension">
            <xsd:any processContents="skip" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)

遵循此模式,我有一个以下类型的XML文档:

<Attachment>
    <Extension>
        <ReportType1>
            <Name>Report Type 1 Example</Name>
        </ReportType1>
    </Extension>
</Attachment>
Run Code Online (Sandbox Code Playgroud)

我在已编译的DLL中有以下类:

public class Attachment
{
    public Extension Extension { get; set; }
}

public class Extension
{
    [XmlElement(ElementName = "ReportType1", IsNullable = false)]
    public ReportType1 ReportType1 { get; set; }

    [XmlElement(ElementName = "ReportType2", IsNullable = false)]
    public …
Run Code Online (Sandbox Code Playgroud)

c# wcf xml-serialization datacontractserializer .net-4.5

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

-Info类名后缀的替代方法

我一直在读Robert C. Martin的清洁代码,并且发现了臭名昭着的声明:

避免使用类名称中的管理器,处理器,数据或信息等字样.

所以,很自然地,我试图-Info从我的一个班级名称中分解出来.现在,我已经看到了各种StackOverflow问题,询问在-Manager或的情况下该怎么做-Processor.我看到评论表明他们无法想象-Data一个好的班级名称.嗯,在我看来,-Data-Info似乎更难分解出.特别是,例如在下面的类中.

我有一个Server类如下:

public class Server {
    //What I would call ServerInfo
    private int id;
    private String name;
    private String address;
    private int port;
    private int connections;
    private int maxConnections;
    private int status;

    //Bunch of members that aren't ServerInfo, for example:
    private ConcurrentHashMap<String, File> files = new ConcurrentHashMap<String, File>();
    private List<String> filePaths = new List<String>();
    /* ... */

    public …
Run Code Online (Sandbox Code Playgroud)

java naming-conventions code-cleanup

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

是否可以根据Accept标头的媒体类型在.NET MVC中选择具有AttributeRouting的Action?

我想根据Accept标头中请求的媒体类型选择我的Controller的Action.

例如,我有一个名为subject的资源.其分配的路线是:

GET /臣//subjectId:int}

通常,浏览器正在请求text/html,这很好.默认的Media Formatter可以处理这个问题.

现在,我有一个自定义逻辑,我希望在访问同一路径时使用一个接受标头指定application/pdf为接受的媒体类型.

我可以创建一个自定义媒体格式化程序,但是,据我所知,这意味着任何设置为Accept标头的请求路径application/pdf也将通过此媒体格式化程序运行.这是无法接受的.

在Java中,有一个名为的注释@Produces:

@Produces注释用于指定资源可以生成并发送回客户端的MIME媒体类型或表示.如果在类级别应用@Produces,则资源中的所有方法都可以默认生成指定的MIME类型.如果在方法级别应用,则注释将覆盖在类级别应用的任何@Produces注释.

这将允许我执行以下操作:

namespace MyNamespace
{
    [RoutePrefix("subjects")]
    public class SubjectsController : Controller
    {
        [Route("{subjectId:int}")]
        [HttpGet]
        public ActionResult GetSubject(int subjectId)
        {
        }

        [Route("{subjectId:int}")]
        [HttpGet]
        [Produces("application/pdf")]
        public ActionResult GetSubjectAsPdf(int subjectId)
        {
            //Run my custom logic here to generate a PDF.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,我找不到.NET中的Produces属性,所以这不起作用.我也找不到类似的属性.

我当然可以手动检查动作正文中的标题,并将其重定向到另一个动作,但这似乎充其量只是hackish.

.NET 4.5中是否有一种机制可以用来解决我忽略或丢失的问题?

(我正在使用NuGet存储库中的MVC 5.2.2)

asp.net-mvc .net-4.5 attributerouting asp.net-mvc-5

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

jQuery.offset()设置position:absolute.我需要的位置:固定

使用它设置元素的偏移量时jQuery.offset({coords}),还会将CSS属性设置positionabsolute.

然而,我有一个div,我设置position: fixed在我的CSS中,我希望它保持这种方式,即使在使用jQuery设置元素的偏移量之后.

现在,我敢肯定,我大概可以设置偏移,则设定position: fixed了,但我不知道是否有一种方法,我可以告诉jQuery来的位置设置为固定的,而不是绝对的,当它设置偏移.

HTML

<div class="searchResults">
    ...
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

DIV.searchResults {
    position: fixed;
    padding: 20px;
    background-color: red;
    z-index: 501;
}
Run Code Online (Sandbox Code Playgroud)

jQuery的

$("DIV.searchResults").offset({left: 0, top: 0});
Run Code Online (Sandbox Code Playgroud)

呈现HTML

<div class="searchResults" style="position: absolute; top: 0px; left: 0px;">
    ...
</div>
Run Code Online (Sandbox Code Playgroud)

显然,由于jQuery在样式中设置位置,它将胜过我的CSS类的值.所以,我需要一种方法来告诉jQuery的设置positionfixed代替absolute,或者告诉它设置偏移不设置CSS属性的值position.

javascript jquery

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

构造函数依赖注入的循环依赖性

我在一个项目中使用Netty(4.0.4.Final),并且我一直在遇到一个我需要考虑的循环依赖.这个问题主要涉及分解循环依赖的概念,但我会在熟悉的人中使用一些Netty术语.既然我的问题实际上并不在于Netty,我决定不对其进行标记.

下面,我发布了我的代码,省略了我认为不相关的部分.

情况

我有一个MyServer类添加ChannelInboundHandlerAdapterBootstrap:

public class MyServer extends AbstractMyServer {
    private Integer someInteger; //Using Integer just for example's sake.

    public MyServer(MyServerInitializer initializer) {
        //...
        bootstrap.handler(initializer);
        //...
    }

    public void updateInteger(Integer value) {
        someInteger = value;
        //Send an update packet to another server.
    }
}
Run Code Online (Sandbox Code Playgroud)

MyServerInitializer需要添加ChannelInboundHandlerAdapterChannelPipeline:

public class MyServerInitializer extends ChannelInitializer<SocketChannel> {
    private ChannelInboundHandlerAdapter handler;

    public MyServerInitializer(ChannelInboundHandlerAdapter handler) {
        this.handler = handler;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception …
Run Code Online (Sandbox Code Playgroud)

java factory circular-dependency code-cleanup

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

Guice在不使用@Singleton的情况下将单个实例注入多个对象

我正在阅读Guice文档,并且遇到了一个标记为" 消除循环(推荐)"的部分,这一部分引起了我的兴趣,因为这正是导致我今天阅读文档的问题.

基本上,为了消除循环依赖关系,您可以"将依赖关系案例提取到一个单独的类中".好的,那里没什么新鲜的.

所以,在这个例子中,我们有.

public class Store {
        private final Boss boss;
        private final CustomerLine line;
        //...

        @Inject public Store(Boss boss, CustomerLine line) {
                this.boss = boss; 
                this.line = line;
                //...
        }

        public void incomingCustomer(Customer customer) { line.add(customer); } 
}

public class Boss {
        private final Clerk clerk;
        @Inject public Boss(Clerk clerk) {
                this.clerk = clerk;
        }
}

public class Clerk {
        private final CustomerLine line;

        @Inject Clerk(CustomerLine line) {
                this.line = line;
        }

        void doSale() {
                Customer …
Run Code Online (Sandbox Code Playgroud)

java guice cyclic-dependency

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

接口没有声明它会抛出,但文档说它可以抛出

(它确实扔了)

根据我使用Java的经验,如果你Exception在一个实现接口的类的方法中抛出一个,那么你在接口上覆盖的方法也必须声明它抛出了Exception.

例如,请考虑以下最小示例:

public interface MyInterface {
    void doSomething() throws IOException;
}


public class MyClass implements MyInterface {
    @Override
    public void doSomething() throws IOException {
        throw new IOException();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我注意到java.nio ByteBuffer.get()没有声明它抛出任何异常:

public abstract byte get();
Run Code Online (Sandbox Code Playgroud)

但是,它的文档说明如下:

Throws:
    BufferUnderflowException If the buffer's current position is not smaller than its limit
Run Code Online (Sandbox Code Playgroud)

然后我检查了执行HeapByteBuffer.get():

public byte get() {
    return hb[ix(nextGetIndex())];
}
Run Code Online (Sandbox Code Playgroud)

在那里我们发现nextGetIndex()哪个实际上是抛出的方法,BufferUnderflowException顺便说一句,也没有声明throws BufferUnderflowException:

final int nextGetIndex() {                          // package-private
    if …
Run Code Online (Sandbox Code Playgroud)

java exception

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

如何使用react-router设置默认子路由?

我正在使用React-Router 1.0.0-rc3,但我无法理解如何为子路由设置默认路由.

我的rootRoute设置如下:

var rootRoute = {
    component: 'div',
    childRoutes: [{
        path: '/',
        component: require('./components/Console'),
        childRoutes: [
            require('./routes/FileManager'),
            require('./routes/UserManager')
        ]
    }]
};

React.render(<Router routes={rootRoute}/>, document.body);
Run Code Online (Sandbox Code Playgroud)

我的控制台根组件设置如下:

module.exports = React.createClass({

    render: function () {
        return (
            <div className="console">
                <ConsoleHeader/>
                {this.props.children}
            </div>
        );
    }

});
Run Code Online (Sandbox Code Playgroud)

我的FileManager路由定义如下:

module.exports = {
    path: 'files',

    getComponent: function (location, callback) {
        callback(null, require('./components/FileManager'));
    }
};
Run Code Online (Sandbox Code Playgroud)

我的目标是在其任何子路由处于活动状态时始终加载控制台组件.它将包含基于子路由的子组件.我想要异步加载子组件.如果没有选择子路由,我希望FileManager子路由也是默认路由.

所以以下两个路由都会选择FileManager组件:

/
/files
Run Code Online (Sandbox Code Playgroud)

我知道这样做的一种方法是做这样的事情:

{this.props.children || require('./routes/FileManager/components/FileManager')}
Run Code Online (Sandbox Code Playgroud)

然而,这感觉不对.我不想从路由定义文件中获取FileManager组件的路径.

也许我可以创建一个默认的子路由条目,如:

{
    path: '/', //Or maybe empty string here since it …
Run Code Online (Sandbox Code Playgroud)

reactjs react-router

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