我已经创建了一个类并导入了我的接口,但是如何完成剩下的工作,我该怎么办?
这里我有什么,但它不起作用我的意思是ClassCastExeption不起作用
代码示例:
import java.util.LinkedList;
import java.util.Queue;
import com.revmetrix.code_test.linkify_queue.ProcessingQueue;
import com.revmetrix.code_test.linkify_queue.ProcessingQueueFactory;
public class Solution {
ProcessingQueue newQueue;
ProcessingQueueFactory runFactory;
Solution() {
ProcessingQueueFactoryClass runFactory = new ProcessingQueueFactoryClass();
ProcessingQueue newQueue = runFactory.createQueue();
}
/**Your ProcessingQueueFactory must contain two methods: one for creating new
* queues and one for cleaning up after them. In `createQueue`, just create a
* new ProcessingQueue, performing any necessary initialization of the queue
* before it is returned. `createQueue` will be called multiple times during our
* automated tests. In `stopQueue`, perform any cleanup required for a queue
* created by your `createQueue` method. (E.g., stop threads, if necessary for
* your solution.) `stopQueue` will be called once for each queue created with
* `createQueue`.
*/
class ProcessingQueueFactoryClass implements ProcessingQueueFactory {
public void stopQueue(ProcessingQueue p) {
}
// TODO encok burda sikinti var, queue yaratacam ama Proccesing Queue donderiyor bu
// asil eleman ekleyecegim queue yi ProcessingQueue nin icinde mi yaratcam?
// ProcessingQueue bi interface bu arada, bunu implement eden class ProcessingQueueClass yazdim
// onun icinde queue olsun dedim yine gormuyor zalim queue olarak
//bi loop var ProcessingQueue ile ProcessingQueueFactory arasinda, anlamadim!
public ProcessingQueue createQueue() {
Queue<String> newQueue = new LinkedList<String>();
return (ProcessingQueue) newQueue;
}
}
/**
* Your ProcessingQueue implementation will receive unprocessed textual data
* from multiple concurrent producers through its `offer` method. Your queue
* must provide a transformed version of the data via its `poll` method. The
* transformation is described below. As a queue, the data received from
* `poll` must be FIFO (first-in, first-out) with respect to calls to
* `offer`; i.e., the first items in should also be the first items out. If
* data is available, `poll` must remove it from queue and return it. If no
* data is available, then `poll` must return null.
*/
class ProcessingQueueClass implements ProcessingQueue {
ProcessingQueueFactoryClass openFactory = new ProcessingQueueFactoryClass();
ProcessingQueue newQueue = openFactory.createQueue();
/**The linkify transformation should find raw URLs prefixed with "http(s)://" in
* the input text and convert them to HTML links. For example,
* http://www.example.com becomes <a href="http://www.example.com">www.example.com</a>. Do not include the scheme
* (http(s)://) in the anchor text. Any URLs that are already within HTML links
* should not be modified. You can assume the input text contains multiple words
* separated by white space (i.e. spaces, new lines) or punctuations (commas,
* periods, etc.)
*/
public String linkifyTransformation(String s){
// System.out.println(s);
String[] splitArray = s.split(" ");
String transformedString = "";
for (int i = 0; i < splitArray.length; ++i) {
if (splitArray[i].startsWith("https://")) {
transformedString += "<a href=\"" + splitArray[i] + "\">"
+ splitArray[i].substring(8) + "</a> ";
} else if (splitArray[i].startsWith("http://")) {
transformedString += "<a href=\"" + splitArray[i] + "\">"
+ splitArray[i].substring(7) + "</a> ";
} else {
transformedString += splitArray[i] + " ";
}
}
//System.out.println(transformedString);
return transformedString;
}
public boolean offer(String s) {
//returns transformedString
linkifyTransformation(s);
// we need to add the transformedString to our Queue, Where should we create?;
//how to return true or false?
return false;
}
public String poll() {
return "";
}
}
public static void main(String[] args) {
// String s = "The quick http://www.brown.com/fox
// jumps https://over.com the lazy dog foo www.bar.com
// is <a href=\"http://myfavorite.com\">my favorite</a> "
// + "These aren't the droids you're looking for.";
//Solution sol = new Solution();
//ProcessingQueueClass pqs = sol.new ProcessingQueueClass();
//pqs.linkifyTransformation(s);
}
}
以下是一些可能有用的细节。
Revmetrix Linkify Queue Coding通过转换解决字符串数据的输入和输出问题。这个队列必须
正确处理具有合理性能的并发操作。从JAR实现ProcessingQueue和ProcessingQueueFactory。
您的ProcessingQueue实现将通过其offer
方法从多个并发生成器接收未处理的文本数据。你的队列
必须通过其poll
方法提供数据的转换版本。作为队列,从poll
收到的数据
对于offer
的调用,必须是FIFO(先进先出);
答案 0 :(得分:0)
通常,当您在java中implement
interface
时,您需要实现它的方法并为该接口中定义的方法定义主体。
如果您不想实现该接口的所有方法体,则可以将您的类更改为abstract
类,并将该方法标记为abstract
。
我不知道您对java中interfaces
和classes
的熟悉程度,但我认为阅读http://tutorials.jenkov.com/java/interfaces.html之类的教程会有所帮助。
[更改问题后编辑]
ClassCastException
发生在这里:
return (ProcessingQueue) newQueue;
因为您非法将Queue<String>
的实例强制转换为ProcessingQueue
类型的接口。这种强制转换是不可能发生的,因为Queue<String>
或者它的父母都没有实现ProcessingQueue
接口。
但是由于你的作业规范,你的实施是不对的。
你应该实现一个类来实现ProcessingQueue
而另一个类来实现ProcessingQueueFactory
(现在你完成了这个部分)。
您不应在ProcessingQueueFactoryClass
课程中创建ProcessingQueue
和ProcessingQueueClass
的实例,因此请删除这两行:
ProcessingQueueFactoryClass openFactory = new ProcessingQueueFactoryClass();
ProcessingQueue newQueue = openFactory.createQueue();
另外,您应该在Queue
课程中保留Strings
的{{1}}。因此,请将以下行从ProcessingQueue
方法移至public ProcessingQueue createQueue()
类:
ProcessingQueue
到
Queue<String> newQueue = new LinkedList<String>();
在class ProcessingQueueClass implements ProcessingQueue {
Queue<String> newQueue = new LinkedList<String>();
...
}
中,您应该得到public boolean offer(String s)
的结果并将其添加到linkifyTransformation(s);
以及删除的newQueue
方法中然后返回 public String poll()
的第一项以维持newQueue
范例。
现在关于FIFO
:
我这堂课你有两个简单的方法。 ProcessingQueueFactoryClass
方法的工作非常简单:只需创建createQueue()
类的实例并将其返回。
另一种方法ProcessingQueueClass
:此方法获取stopQueue(ProcessingQueue p)
的实例作为参数。由于您的类ProcessingQueue
已实现ProcessingQueueClass
,因此输入参数的运行时类型将为ProcessingQueue
类型。并且因为您的ProcessingQueueClass
类中有Queue<String>
成员变量,此方法的目的是清除ProcessingQueueClass
项并将其释放。
希望这会有所帮助,
祝你好运。