所以情况就是这样.我想像这样定义一个case类:
case class A(val s: String)
Run Code Online (Sandbox Code Playgroud)
我想定义一个对象,以确保在创建类的实例时,'s'的值始终为大写,如下所示:
object A {
def apply(s: String) = new A(s.toUpperCase)
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为Scala抱怨apply(s:String)方法定义了两次.我理解case类语法会自动为我定义它,但是我不能用另一种方法来实现它吗?我想坚持使用case类,因为我想将它用于模式匹配.
我很好奇Scala是否在我可以使用的集合类中隐藏了一些gem.基本上我正在寻找类似FIFO队列的东西,但是它的大小有一个上限,这样当命中限制时,最旧的(第一个)元素将从队列中删除.我以前用Java自己实现了这个,但如果可能的话,我宁愿使用标准的东西.
我有一个列表,我想以类似于(partition sz step col)Clojure方法或IterableLike.sliding(size: Int, step: Int)Scala函数的方式进行拆分.具体来说,给出如下列表:
(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
我希望能够迭代子列表,如:
(1, 2), (2, 3)
Run Code Online (Sandbox Code Playgroud)
在Clojure中,这将完成:
(partition 2 1 (1, 2, 3))
Run Code Online (Sandbox Code Playgroud)
和Scala一样,它将是:
val it = Vector(1, 2, 3).sliding(2)
Run Code Online (Sandbox Code Playgroud)
然而,我没有这么奢侈,我希望避免不得不自己动手.Guava有一个接近的分区方法,但不提供重叠.谷歌搜索也没有结果.这种方法是存在还是我必须自己滚动?
我正在使用Apache的Oltu库,我正在尝试使用OAuth2通过Google进行身份验证.这是相关的代码:
OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
OAuthClientRequest clientReq = OAuthClientRequest
.tokenProvider(OAuthProviderType.GOOGLE)
.setClientId("<my-client-id>")
.setClientSecret("<my-client-secret>")
.setRedirectURI("https://test.example.com/oauthtest/oauth/google/auth")
.setCode(oar.getCode())
.setGrantType(GrantType.AUTHORIZATION_CODE)
.buildQueryMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
// This call fails with the OAuthProblemException
OAuthAccessTokenResponse oAuthResponse = oAuthClient.accessToken(clientReq,
OAuthJSONAccessTokenResponse.class);
Run Code Online (Sandbox Code Playgroud)
我可以通过Facebook进行身份验证而不会出现问题,但无论出于何种原因,这都是失败的.我可以毫无问题地从OAuthAuthzResponse获取代码,因此我知道原始呼叫正在运行,但此后续呼叫失败.
编辑:我已经放弃使用Oltu并坚持使用HttpClient库并手动执行OAuth舞蹈的简单方法.它对我来说效果更好,我会推荐给任何想要可靠地对多个OAuth提供商进行身份验证的人.只有Twitter要求我使用Scribe.
我在Tomcat 7上使用Hibernate 4.0和JPA persistence.xml文件.没有Struts,只是直接使用一些Jersey服务的Hibernate.这是我遇到的例外:
Caused by: org.hibernate.service.jndi.JndiException: Unable to lookup JNDI name [jdbc/MyDB]
at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:68)
at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:116)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2273)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2269)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1738)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
... 8 more
Caused by: javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:135)
at javax.naming.InitialContext.lookup(InitialContext.java:396)
at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:65)
... 23 more
Run Code Online (Sandbox Code Playgroud)
我看到关于jbc的注释在这种情况下没有约束,但我很困惑这是如何发生的.我正在特定于应用程序的context.xml中部署我的上下文,如下所示:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/MyDB" auth="Container" …Run Code Online (Sandbox Code Playgroud) 当我尝试执行以下操作时,我在Scala中遇到奇怪的类型不匹配错误:
val m = Map[String, Int]("a" -> 1, "b" -> 2, "c" -> 3)
val n = Map[String, Int]("c" -> 3, "d" -> 4, "e" -> 5)
n.filter((k: String, v: Int) => !m.contains(k))
<console>:10: error: type mismatch;
found : (String, Int) => Boolean
required: (String, Int) => Boolean
n.filter((k: String, v: Int) => !m.contains(k))
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?类型不匹配在这里没有意义.
scala ×4
java ×2
case-class ×1
clojure ×1
collections ×1
dictionary ×1
filter ×1
google-oauth ×1
guava ×1
hibernate ×1
jndi ×1
jpa ×1
oauth-2.0 ×1
oltu ×1
tomcat ×1
types ×1